AWK Examples
As a second example, suppose you have several thousand files you want to move into a new directory and rename by appending a .dat to the filenames. You could do this one by one (several hours), or use vi to make a decent command file to do it (several minutes), or use awk (several seconds).Suppose the files are named junk* (* is wildcard for any sequence of characters), and need to be moved to ../iraf and have a '.dat' appended to the name. To do this type
ls junk* | awk '{print "mv "$0" ../iraf/"$0".dat"}' | csh
Print first two fields in opposite order:
awk '{ print $2, $1 }' file
Print lines longer than 45 characters
awk 'length > 45' file
Print length of string in 4th column
awk '{print length($4)}' file
Add up first column, print sum and average
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
Print fields in reverse order
awk '{ for (i = NF; i > 0; --i) print $i }' file
Print the last line
{line = $0}
END {print line}
Print the total number of lines that contain the word Mike
/Mike/ {nlines = nlines + 1}
END {print nlines}
Print all lines between start/stop pairs
awk '/start/, /stop/' file
Print all lines whose first field is different from previous one
awk '$1 != prev { print; prev = $1 }' file
Print column 3 if column 1 > column 2
awk '$1 > $2 {print $3}' file
Print line if column 3 > column 2
awk '$3 > $2' file
Count number of lines where col 3 > col 1
awk '$3 > $1 {print i + "1"; i++}' file
Print sequence number and then column 1 of file
awk '{print NR, $1}' file
Print every line after erasing the 2nd field
awk '{$2 = ""; print}' file
Print hi 28 times
yes | head -28 | awk '{ print "hi" }'
Print hi.0010 to hi.0099
yes | head -90 | awk '{printf("hi00%2.0f n", NR+9)}'
Print out 4 random numbers between 0 and 1
yes | head -4 | awk '{print rand()}'
Print out 40 random integers modulo 5
yes | head -40 | awk '{print int(100*rand()) % 5}'
Replace every field by its absolute value
{ for (i = 1; i <= NF; i=i+1) if ($i < 0) $i = -$i print}
If you have another character that delimits fields, use the -F option
For example, to print out the phone number for Jones in the following file
# 000902|Smith|Theodore|333-242-2222|149092
# 000901|Jones|Bill|532-382-0342|234023
# ...
# type
awk -F"|" '$2=="Jones"{print $4}' filename
Some looping commands
Remove a bunch of print jobs from the queue
BEGIN{
for (i=175;i>133;i--){
printf "lprm -Plw %dn", i
} exit
}
