This is a small collection of script snippets, some are more interesting then others. An old but similar page I created is located here..
Contend is moved to PerlScripting
Using ex editor (basis of vi commands) to change sh-bang
NEWBIN="/home/root/testperl"
ex -c"1,1s%/usr/bin/perl%${NEWBIN}%" -c"wq" myscript.pl
Strip comments, strip leading spaces, strip trailing whitespaces and remove empty lines
sed ' /#.*$/s/// s/^[[:blank:]]\+// s/[[:blank:]]\+//g /^$/d ' # or use a similar short hand sed -n '/^[0-9|A-Z|a-z]/p' # simple delete sed /^$/d ## same as grep -v ^$ # escape spaces and hypens echo "da da - lala" | sed 's/[\ |\-]/\\&/g' da\ da\ \-\ lala # remove most HTML tags (accommodates multiple-line tags) sed -e :a -e 's/<[^>]*>//g;/</N;//ba' # remove last line | sed '$d'
# How do i log everything to a file in a script?
exec >>/tmp/logfile 2>&1
# Howto use cat as an editor?
cat << EOF >> /tmp/file.out
somme crappy text
EOF
# difference beween column selections
file is 221932KiB and has 17481202 lines
$ time cat file > /dev/null
real 0m0.36s
$ time cat file | awk '{print $2}' > /dev/null
real 0m23.03s
$ time cat file | tr -s ' ' ';' | cut -d";" -f 2 > /dev/null
real 0m18.04s # tr-command takes 2x longer then cut-command
$ time perl -p -e 's/ +/ /' file | cut -d" " -f 2 > /dev/null
real 0m47.67s
$ time perl -lane 'print $F[1]' file > /dev/null
real 2m34.27s
$ time perl -lape '$_ = $F[1]' file > /dev/null
real 2m46.11s
# change capitals
$ tr '[[:upper:]]' '[[:lower:]]'
# Adding numbers in column layouts using awk:
ipcs -m -b | sed '1,3d' | awk '{SIZE+=$7} END {print SIZE}'
ipcs -m -b | sed '1,3d' | perl -lane '$sum += $F[6]; END {print $sum}'
# Grep for a field, not the full line:
ls -l | awk '$6 == "Dec"'
# Get last argument
echo "number of arguments: $# "
eval LAST=$"$#"
echo "last argument: $LAST "
MAXSHIFT=$(( $# - 1 ))
echo "maximum shifts: $MAXSHIFT "
shift $MAXSHIFT
echo "last argument after maximum shifts: $1 "
# Array loop
$ colors[0]=RED ; colors[1]=GREEN ; colors[2]=BLUE
$ set -A arrray a b c d
$ i=0
$ while [ $i -lt ${#colors[*]} ]
> do
> print ${colors[$i]}
> (( i=i+1 ))
> done
$ echo list all elements ${arrray[*]} (use * or @)
I don't use awk for scripting and thus always try to find alternatives for my 1-liners. Maybe it is a shame, awk is quite performant.
# Adding numbers in column layouts
ipcs -m -b | sed '1,3d' | awk '{SIZE+=$7} END {print SIZE}'
ls -l | awk '$6 == "Dec"'
cat file | awk '{print $2}'
# Converting scientific notation
echo "5.083E+5" | awk -F"E" 'BEGIN{OFMT="%10.10f"} {print $1 * (10 ^ $2)}'
508300
# Find the position of a string
echo "123%%67890" | awk -v find=%% '{ printf ("%s\n",index($0,find) ) }'
# ps: there is also a perl one-liner for this
replacing End of line
blablablah | perl -p -i -e 's/\n/,/g' blablablah | tr '\n' ','
To use CGI outside the Apache /cgi-bin/ in Apache.conf
AddHandler cgi-script .cgi
playfullsimple.cgi
#!/bin/sh
set -x
echo "Content-type: text/html"
echo ""
cat << ++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>title</title></head>
<body>
++++
echo "$( date ) ---- ${QUERY_STRING} <br>"
echo '<hr>'
set
echo '</body>'
echo '</html>'
exit 0
EOF
Note: Do not forget to chown & chmod
# insert $value in file according to Line and Column coördinates (column separator = ":")
awk 'BEGIN { FS=OFS=":" } NR==l { $f=v } 1' l=$LIGNE f=$COLUMN v=$VALUE input_file