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

    If you found this article helpful , please give it a like!
    1
    Loading...
    1 vote, average: 1.00 / 11
    1,026
    X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Junichiro Okazaki

Extensive experience in relocating and operating smartphone games.

He handles multi-cloud operations, server construction and relocation on a daily basis. As the number of cases has increased, I am considering how to improve the efficiency of my work. We often consider methods for relocating servers based on the merits of each cloud.

While we were relocating between clouds and from physical to cloud, we achieved two consecutive victories in a competition held by the Japan MSP Association.