How to use the awk command

table of contents
Hello everyone.
I'm Okazaki from the System Solutions Department.
This time, I'll be writing about the awk command, which is also used when checking logs.
What is the awk command?
This command allows you to output only data from specific columns or calculate total values when processing multiple lines of data separated by spaces or specific characters
awk '{print$[number of columns you want to output];}'
For example, there is a file like this:
test.txt 1 2 3 5 1 2 3 6 1 2 3 7 1 2 3 8
If you want to display only the first column, you can do so as follows:
cat test.txt | awk '{print$1;}' 1 1 1 1
How to use
To use any character as a delimiter:
awk -F [delimiter] '{print$1;}'
- Usage example
Show only the second column
sed -e 's/ /:/g' test.txt | awk -F ':' '{print$2;}' 2 2 2 2
To output line numbers:
awk '{print NR $1;}'
Show only the third column
sed -e 's/ /:/g' test.txt | awk -F ':' '{print NR $3;}' 13 23 33 43
However, this is difficult to understand, so do it like this:
sed -e 's/ /:/g' test.txt | awk -F ':' '{print NR " " $3;}' 1 3 2 3 3 3 4 3
To output rows where a specific column contains a specific string:
awk '$1==[any character]'
Only 5 is displayed on the fourth line
sed -e 's/ /:/g' test.txt | awk -F ':' '$4=="5" {print NR " " $4;}' 1 5
In the opposite case
sed -e 's/ /:/g' test.txt | awk -F ':' '$4!="5" {print NR " " $4;}' 2 6 3 7 4 8
To sum the numbers in a specific column:
awk '{sum+=$1;}END{print sum;}'
Display the total on the first line
sed -e 's/ /:/g' test.txt | awk -F ':' '{sum+=$1;}END{print sum;}' 4
In the case of the average value
sed -e 's/ /:/g' test.txt | awk -F ':' '{sum+=$1;}END{print sum/NR;}' 1
summary
You will likely need to open log files frequently during operations, but
formatting the file before opening it will make it easier to read and
will allow you to more efficiently aggregate errors and accesses.
Using this awk command in conjunction with other commands in your daily operations will help improve the efficiency of your operations.
1