Awk
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
awk
may also be used in functions. In the example below wc
and awk
is used to determine number of lines in a input file.
#!/bin/sh
# Return the length in number of lines of a file ($1)
file_len() { wc -l $1 | awk '{ print $1 }'; }
echo "Input file $1 has `file_len "$1"` lines"
Another useful example for awk
is to scan the dhcpd
output for recent IP allocations. This is useful when you connect a headless server to your network with DHCP and you need to know what IP it recieved.
ssh user@dhcpserver cat /var/log/daemon | grep DHCPACK | tail | awk '{ print "DHCP acknowledged to "$8" at "$1" "$2" "$3 }'
Sometimes you need shell expansion inside an awk script.
date=`date -u "+%Y-%m-%dT%H:%M:%SZ"`; ls | awk '{ print "'"$date"'",$0 }'
See [awk description at wikipedia](http://en.wikipedia.org/wiki/AWK_(programming_language) for more information.
The cut
command can be used as an alternative to awk
. See the following example. The firstcut
command could be replaced with and awk
command instead.
pkg_info | grep xfce | cut -f 1 -d' ' | awk '{ print "pkg_delete -D dependencies "$1 }'