Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the awk command

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge about how to use the awk command. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Awk mode

Awk supports different types of patterns, including regular expressions, relational expressions, ranges, and special expression patterns.

In the following example, a file named "teams.txt" will be created with the following contents:

[root@localhost] # cat teams.txtBucks Milwaukee 60 22 0.732Raptors Toronto 58 24 0.70776ers Philadelphia 51 31 0.622Celtics Boston 49 33 0.598Pacers Indiana 48 34 0.585 regular expression pattern

Awk's regular expression pattern is enclosed in slashes (/ /). For example, to display each record containing "0.5", you can run the following command:

[root@localhost ~] # awk'/ 0.5 / {print $0} 'teams.txtCeltics Boston 49 33 0.598Pacers Indiana 48 34 0.585

To display the first and second fields of each record that contains "0.5":

[root@localhost] # awk'/ 0.5 / {print $1 teams.txtCeltics BostonPacers Indiana 2}'

Display the record at the beginning of one or more numbers and print the first column:

[root@localhost ~] # awk'/ ^ [0-9] / {print $1} 'teams.txt76ers

Relational expression pattern

Relational expression patterns are often used to match the contents of specific fields or variables. Common relational operators are:

~: contains! ~: does not contain >: greater than: less than > =: greater than or equal to: less than or equal to =: equal to! =: not equal to

Use the ~ include comparison operator. For example, display each record that contains the "ia" character in the second field:

[root@localhost ~] # awk'$2 ~ / ia/ {print $0} 'teams.txt76ers Philadelphia 51 31 0.622Pacers Indiana 48 34 0.585

To match records that do not contain a given character, you can use! ~ does not contain an operator. For example, display each record that does not contain the "ia" character in the second field:

[root@localhost ~] # awk'$2! ~ / ia/ {print $0} 'teams.txtBucks Milwaukee 60 22 0.732Raptors Toronto 58 24 0.707Celtics Boston 49 33 0.598

Use the! = "not equal" operator

Use the > = "greater than or equal to" operator to display records with a fourth field greater than or equal to 30:

[root@localhost ~] # awk'$4 > = 30 {print $0} 'teams.txt76ers Philadelphia 51 31 0.622Celtics Boston 49 33 0.598Pacers Indiana 48 34 0.585 range mode

The range pattern consists of two sets of characters separated by commas, starting with the record that matches the first string until it matches the record of the second string.

For example, to display records from "Raptors" to "Celtics":

[root@localhost ~] # awk'/ Raptors/,/Celtics/ {print $0} 'teams.txtRaptors Toronto 58 24 0.70776ers Philadelphia 51 31 0.622Celtics Boston 49 33 0.598

Range mode can also use relational expressions, for example, to display records where the fourth field equals 31 to the fourth field equals 34:

[root@localhost ~] # awk'$4 = = 31, $4 = = 34 {print $0} 'teams.txt76ers Philadelphia 51 31 0.622Celtics Boston 49 33 0.598Pacers Indiana 48 34 0.585 Special expression pattern

Awk includes the following special modes.

BEGIN: do this before processing the record. END: used to do this after the record has been processed.

BEGIN mode is usually used to set variables, and END mode is used to process statistical data. For example, the following will show "Start Processing.", then the third field, and finally "End Processing.":

[root@localhost ~] # awk 'BEGIN {print "Start Processing."}; {print $3}; END {print "End Processing."}' teams.txtStart Processing.6058514948End Processing. Built-in variable

Awk has many built-in variables that allow you to control how the program is handled. Here are some common built-in variables:

NF: number of fields in the record NR: current record number FILENAME: input file name currently being processed FS: field delimiter RS: record separator OFS: output field separator ORS: output record separator

Here is an example of how to print a file name and the number of lines:

[root@localhost] # awk 'END {print "File", FILENAME, "contains", NR, "lines."}' teams.txtFile teams.txt contains 5 lines. These are all the contents of the article "how to use awk commands". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report