Get the App
SLTechnology News&Howtos  ›  Servers  › 

The enlightenment of writing awk commands and scripts

Shulou Source: shulou.com Published: 2022-06-02 06:48:41 09月10日 Update

Awk commands are a powerful way to process or analyze text files, especially data files organized by rows and columns. You can run simple awk commands from the command line. More complex tasks should be written to the file as awk programs (so-called awk scripts).

The basic format of the awk command is as follows:

Awk 'pattern {action}' input-file > output-file

This means that each line of the input file is fetched; if the line contains the pattern, the operation is applied to the line and the resulting line is written to the output file. If the mode is omitted, the action is applied to all rows. For example:

Linuxidc@Ubuntu:~/linuxidc.com$ awk'{print $5} 'linuxidc.txt > linuxmi.txt

This statement takes the element in column 5 of each row and writes it as a line to the output file "output.txt". The variable'$4 'refers to the fourth column. Similarly, you can access the first, second, and third columns using $1, 2, and 3, and so on. By default, it is assumed that columns are separated by spaces or tabs (so-called spaces). Therefore, if the input file "linuxidc.txt" contains the following line:

1, linuxidc com, Title 599, Price $9.30

2, ninhao linuxidc, Title 761, Price $5.90

3, naike xie, Title 880, Price $9.10

4, Lady Gaga, Title 198, Price $1.30

5, Johnny Cash, Title 582, Price $6.50

6, Elvis Presley, Title 370, Price $9.30

7, John Lennon, Title 571, Price $8.90

8, Michael Jackson, Title 673, Price $7.50

9, linuxidc com, Title 585, Price $1.80

The command then writes to the output file "linuxmi.txt" with the following line:

Linuxidc@ubuntu:~/linuxidc.com$ cat linuxmi.txt599761880198582370571673585

If the column delimiter is not a space or a tab, such as a comma, you can specify it in the awk statement, as follows:

Linuxidc@ubuntu:~/linuxidc.com$ awk-F,'{print $3} 'linuxidc.txt > linuxmi.txt

If you think the columns are separated by commas, this selects the element from the third column of each row. Therefore, in this case, the output will be:

Linuxidc@ubuntu:~/linuxidc.com$ cat linuxmi.txt Title 599 Title 761 Title 880 Title 198 Title 582 Title 370 Title 571 Title 673 Title 585

The list of statements in curly braces ('{','}') is called a block. If you place a conditional expression in front of a block, statements within the block are executed only if the condition is true.

Awk'$7 linuxidc.txt = "\ $7.30" {print $3} 'linuxidc.txt

In this case, the condition is $7 percent = "\ $7.30", which means that the element in column 7 is equal to $7.30. The backslash in front of the dollar sign is used to prevent the system from interpreting $7 as a variable, but to take the dollar sign literally.

Therefore, this awk statement prints out the element in the third column of each row, the "$7.30" in the seventh column.

You can also use regular expressions as conditions. For example:

Linuxidc@ubuntu:~/linuxidc.com$ awk'$7 linuxidc.txt = "\ $7.30" {print $3} 'linuxidc.txt

The string between two slashes ('/') is a regular expression. In this case, it's just the string "30". This means that if a line contains the string "30", the system will print the element in the third column of the line. The output in the above example will be:

Linuxidc@ubuntu:~/linuxidc.com$ awk'/ 30 / {print $3} 'linuxidc.txtcom,Gaga,Presley

If the table elements are numbers, awk can run calculations on them, as shown in the following example:

Linuxidc@ubuntu:~/linuxidc.com$ awk'{print ($2 * $3) + $7} 'linuxidc.com.txt

In addition to variables that access the current row element ($1, NF, etc.), there is also the variable $0 for the full row (row) and the variable for the number of fields.

You can also define new variables in this example:

Linuxidc@ubuntu:~/linuxidc.com$ awk'{sum=0; for (col=1; col

Tags: Element file command variable statement output case condition line character string meaning space expression tab slash pattern regular symbol system Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia Shulou Technology Linux MariaDB