Aho Weinberger Kernighan
awk
is a full scripting language, as well as a complete text manipulation toolkit for the command line.
Use
who | awk '{print $1}' print the first field of every line from the who command
who | awk '{print $1,$4}' print the first and fourth field of every line from the who command
who | awk '{print $NF}' print the last field (NF=Number of Fields) of every line from the who command
date | awk 'OFS="-" {print$2,$3,$6}' print the date fields 2, 3 and 6 using the OFS (Output Field Seperator) '-'
date | awk 'OFS="/" {print$2,$3,$6}' print the date fields 2, 3 and 6 using the OFS (Output Field Seperator) '/'
awk 'BEGIN {print "yoloooo"} {print $0} END {print "swaaag"}' test.sh 'BEGIN' prints "yoloooo" before any text processing, 'END' prints "swaaag" after text processing
awk -F: '$3 >= 1000 {print $1,$6}' /etc/passwd print field 1 and 6 from /etc/passwd where field 3 is greater than 1000
Useful Flags
-F: define field delimiter as ':'