[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

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

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

    If you found this article helpful , please give it a like!
    1
    Loading...
    1 vote, average: 1.00 / 11
    871
    X facebook Hatena Bookmark pocket
[2026.6.30 Amazon Linux 2 end of support] Amazon Linux server migration solution

[2026.6.30 Amazon Linux 2 end of support] Amazon Linux server migration solution

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.