How to use the awk command

table of contents
Hello everyone.
This is Okazaki from the System Solutions Department.
This time, I'll be writing about the awk command, which is often used in conjunction with checking logs.
What is the awk command?
you to process multi-line data separated by spaces or specific characters,
outputting only the data from a specific column, or calculating the sum of the values.
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
In operations, you'll likely open log files quite often, but
formatting them before opening them makes them much easier to read and
allows for much more efficient aggregation of errors and access data.
Combining awk with other commands, as demonstrated here, can significantly improve the efficiency of your daily operations.
1
