How to use the awk command
Hello.
My name is Okazaki from the System Solutions Department.
This time, I will write about the command awk, which is used when checking logs.
What is the awk command?
This command allows you to output only the data in a specific column or calculate the total value when processing multiple lines of data separated by spaces or specific characters
awk '{print$[number of columns you want to output];}'
For example, I have a file like the one below.
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 output it as follows.
cat test.txt | awk '{print$1;}' 1 1 1 1
About how to use
When using any character as a delimiter
awk -F [delimiter] '{print$1;}'
- Usage example
Show only 2nd column
sed -e 's/ /:/g' test.txt | awk -F ':' '{print$2;}' 2 2 2 2
When outputting line numbers
awk '{print NR $1;}'
Display only 3rd column
sed -e 's/ /:/g' test.txt | awk -F ':' '{print NR $3;}' 13 23 33 43
However, this is difficult to understand, so do the following
sed -e 's/ /:/g' test.txt | awk -F ':' '{print NR " " $3;}' 1 3 2 3 3 3 4 3
When outputting rows where a specific column has a specific string
awk '$1==[any character]'
Only 5 is displayed on the 4th 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
If you want to sum numbers in a particular column
awk '{sum+=$1;}END{print sum;}'
Display the total of the first line
sed -e 's/ /:/g' test.txt | awk -F ':' '{sum+=$1;}END{print sum;}' 4
For the average value
sed -e 's/ /:/g' test.txt | awk -F ':' '{sum+=$1;}END{print sum/NR;}' 1
summary
I think it is quite common to open log files during operation, but if you
format the file and open it, it will be easier to see, and
I think it will be much more efficient to tally errors and accesses.
I think that if you use this awk in conjunction with other commands in your daily operations, you can make your operations more efficient.