A typical use of the awk programming language. Seems really useful for quick sorting a filtering directly from command line.
The following command list all files with .java ending in current folder and decendent folders. Print all of them and the number of them found. The description in english is actually longer that the actual program.
$ ls -R | awk 'BEGIN{sum=0}; /.java/ {print;sum += 1}; END {print "num=",sum}'
awk is also very useful for filtering delimited output. The following example filters out a subset of the columns from a df output.
df -h | awk '{print $1,$2,$6}'
Add wget string in front of each line in downloads.txt file.
cat downloads.txt | awk '{print "wget",$0}' > download.sh # OR
awk '{print "wget",$0}' downloads.txt > download.sh
Count number of shell variants used on a system. -F is used to set delimiter. sort and uniq to count.
awk -F : '{ print $7 }' /etc/passwd | sort | uniq -c
Delete all packages on OpenBSD.
pkg_delete -i `pkg_info | awk '{print $1}'`
Or you can user xargs.
pkg_info | awk '{ print $1 }' | xargs pkg_delete -i
List package dependencies. Note that pkg_info -t may be used to find packages with no dependence.
pkg_info | awk '{print $1}' | xargs pkg_info -R
See awk description at wikipedia for more information.