How to use the awk command

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;}'
    
  • 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

    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.

    If you found this article useful, please click [Like]!
    1
    Loading...
    1 vote, average: 1.00 / 11
    1,075
    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