AWK Print the length of the longest input line: awk '{if (length($0) > max) max = length($0) } END { print max}' data Print every line longer than 80 characters: awk 'length($0) > 80' data Print the length of the longest line in data: expand data | awk '{if (x < length()) x = length() } END { print "maximum line length is " x}' Print every line that has at least one field awk 'NF > 0' data Print seven random number from 0 to 100 inclusive awk 'BEGIN{ for (i = 1; i <= 7; i++) print int(101 * rand()) }' Print the total number of bytes used by files ls -l files | awk '{ x += $5 } END { print "total bytes: " x }' Print a sorted list of login names of all users awk -F: '{ print $1 }' /etc/passwd | sort Count lines in a file awk 'END { print NR}' data Print root mean square deviation awk '{ x += ($1-$2)^2}{print (x/NR)^0.5}' data Print formated awk '/Search/{printf("%14.8f %14.8f\n",($3*4),($4*2))}' Print an increasing number up to 60 awk 'BEGIN { for ( i=1 ; i <= 60 ; i++ ) print i, 1, 1, 0 }' Print sum of two numbers in same line together with complete line echo 3 4 | awk '{ for (i=1; i<=NF;i++) xyz = xyz + $i} END {print xyz,$0}' Print running average of first colum awk '{ x += $1}{print (x/NR)}' data Print first some headings, then $1 in list.dat as a string of 10 characters that are left-justified. awk 'BEGIN { printf "%-10s %s\n", "Name", "Number"} { printf "%-10s %s\n", $1, $2 }' list.dat printf "%d", 99/2 49 printf "%e", 99/2 4.950000e+01 printf "%f", 99/2 49.500000 printf "%6.2f", 99/2 49.50 printf "%g", 99/2 49.5 printf "%o", 99 143 printf "%06o", 99 000143 printf "%x", 99 63 printf "|%s|", "January" |January| printf "|%10s|", "January" | January| printf "|%-10s|", "January" |January | printf "|%.3s|", "January" |Jan| printf "|%10.3s|", "January" | Jan| printf "|%-10.3s|", "January" |Jan | printf "%%" % SED deletes everything in between colon and { grep -n "{" file-name | sed 's/*:.*/:/; s/.*{*/{/' replaces pattern by set of variables sed 's/10 10 10/'"$cell $cell $cell"'/' file-name MERGE multiple PDFs into one file gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=firstANDsecond.pdf -dBATCH first.pdf second.pdf backup dir1 into dir2 rsync -avz dir1 dir2