How to use the awk command
table of contents [非表示]
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
1 | awk '{print$[number of columns you want to output];}' |
For example, I have a file like the one below.
1 | 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.
1 | cat test .txt | awk '{print$1;}' 1 1 1 1 |
About how to use
When using any character as a delimiter
1 | awk -F [delimiter] '{print$1;}' |
- Usage example
- Usage example
- Usage example
- Usage example
Show only 2nd column
1 | sed -e 's/ /:/g' test .txt | awk -F ':' '{print$2;}' 2 2 2 2 |
When outputting line numbers
1 | awk '{print NR $1;}' |
Display only 3rd column
1 | 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
1 | 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
1 | awk '$1==[any character]' |
Only 5 is displayed on the 4th line
1 | sed -e 's/ /:/g' test .txt | awk -F ':' '$4=="5" {print NR " " $4;}' 1 5 |
In the opposite case
1 | 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
1 | awk '{sum+=$1;}END{print sum;}' |
Display the total of the first line
1 | sed -e 's/ /:/g' test .txt | awk -F ':' '{sum+=$1;}END{print sum;}' 4 |
For the average value
1 | 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.