The `awk` Command
Has some crossover abilities but really shines with displaying or translating column data.
Example usage:
Find something in a file, then display the first two columns:
grep '<something>' somewhere.txt | awk '{print $1, $2}'
Output data in a more formatted way:
awk '{printf "%-10s %s\n", $1, $2}' file.txt
RS = record separator (newline)
FS = field separator (all whitespace)
NR = number of records (current index)
NF = number of fields in current record
OFS = output field separator (default is space but for CSV you could use a ,
)
awk 'BEGIN {OFS=","} {print $1, $2}' file.txt
ORS = output record separator (default is newline but could use two newlines or maybe a semicolon, etc)
awk 'BEGIN {ORS="; "} {print $1}' file.txt
ARGV = argument list, 0 is awk
itself. Normal.
ARGC = argument count, not including awk
itself.
FILENAME = current file being processed
ENVIRON = environmental variables, like php's $_SERVER
awk 'BEGIN {print ENVIRON["HOME"]}'
BEGIN / END = special blocks that process before and after the actual output loop. Allow you to output stats or maybe change the record and field variables.
Examples:
git branch --v | grep "\[gone\]" | grep -v 'ha\/' | awk '{print $1}'| xargs git branch -d