Basics of AWK
AWK is a command line tool used for text stream manipulation. It comes by default on most if not all Linux distributions. Awk parses documents using columns.The basic syntax for awk is:
Code [Expanded]:
awk <flags> '{subcommand}' file.txt
In the curly brackets you write what exactly you want awk to do. For example the most basic command is print.
Code [Expanded]:
awk '{print}' file
To parse columns with awk you use $ and the column number. In this example we will use the ps command.
Code [Expanded]:
ps | awk '{print $1}'
If you do not want the separator to be a space you can use the -F flag. in this example we will use /etc/passwd file
Code [Expanded]:
awk -F ':' '{print $1} /etc/passwd
In this file it uses colons to separate the columns. so " -F ':'" tells awk to use colon as the separator. The output of this command will print out all users on the system.
If you are working with larger files and do not want to count to find the last column you can use $NF
Code [Expanded]:
awk -F '/' '{print $NF}' /etc/shells
Awk can even add numbers if you needed it too.
Code [Expanded]:
ps | awk '{s+=$1} END {print s}
this command adds the numbers outputted in the first column of the ps command and saves it to a variable "s". Then it prints the variable "s".
These are some basic things you can do with awk. Feel free to ask any questions and I will get back to you as soon as I can.