How to use the awk command

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;}'
    
  • Usage example
  • 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]'
    
  • Usage example
  • 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;}'
    
  • Usage example
  • 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.

    If you found this article helpful,please give it a "Like"!
    1
    Loading...
    1 vote, average: 1.00 / 11
    1,179
    X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Junichiro Okazaki

Extensive experience in relocating and operating smartphone games

I am currently working on multi-cloud operations, server construction, and relocation. As the number of projects increases, I am considering ways to improve the efficiency of my work. I often consider methods for relocating servers, taking into account the advantages of each cloud

While carrying out migrations between clouds and from physical to cloud, the company won two consecutive championships in competitions held by the Japan MSP Association