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 fields, records, and variables in the awk command

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use the fields, records and variables in the awk command. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Awk is a programming language used to process text and data under linux/unix. The data can come from standard input (stdin), one or more files, or the output of other commands. It supports advanced functions such as user-defined functions and dynamic regular expressions, and is a powerful programming tool under linux/unix.

In most Linux distributions, awk and gawk are soft links to GNU awk. Enter awk to invoke the same command. The full history of awk and gawk can be seen in the GNU awk user manual.

The first article in this series introduced the basic format of the awk command:

$awk [option] 'Mode {Action}' input file

Awk is a command, followed by options (such as using-F to define a field delimiter). The part that you want awk to execute needs to be written between two single quotes, at least in the terminal. In the awk command, to further emphasize the part you want to execute, you can highlight it with the-e option (but this is not necessary):

$awk-F,-e'{print $2;} 'colours.txtyellowbluegreen [...]

Records and fields awk treat the input data as a series of records, usually split by row. In other words, awk treats each line in the text as a record. Each record contains multiple fields. A field is separated by a field delimiter and the field is part of the record.

By default, awk treats various white space characters, such as spaces, tabs, line breaks, and so on, as delimiters. It is worth noting that in awk, multiple spaces are treated as a delimiter. So the following line of text has two fields:

Raspberry red

This line is also:

Tuxedo black

Other delimiters are not handled in this way in the program. Assuming that the field delimiter is a comma, there are three fields for the record shown below. One of the fields may be 0 bytes (assuming this field does not contain hidden characters)

A Bawk program for pencils

The program part of the awk command is made up of a series of rules. Generally speaking, each rule in the program has one line (although this is not necessary). Each rule consists of a pattern, or one or more actions:

Mode {Action}

In a rule, you can determine whether the action will be performed in the record by defining the pattern. Patterns can be simple comparison conditions, regular expressions, or even a combination of both, and so on.

In this example, the program only displays records that contain the word "raspberry":

$awk'/ raspberry/ {print $0} 'colours.txtraspberry red 99

If no text matches the pattern, the action will be applied to all records.

Moreover, when a rule contains only patterns, it is equivalent to performing {print} on the entire record and printing it out.

Awk programs are data-driven in nature, and the result of command execution depends on the data. Therefore, compared with programs in other programming languages, it is still different.

NF variable

Each field has specified variables, but there are also some special variables for fields and records. The NF variable, which stores the number of fields found by awk in the current record. Its contents can be displayed on the screen and can also be used for testing. The data in the following example is from the text of the previous article:

$awk'{print $0 "(" NF ")"} 'colours.txtname color amount (3) apple red 4 (3) banana yellow 6 (3) [...]

Awk's print function takes a series of arguments (which can be variables or strings) and splices them together. This is why, in this example, at the end of each line, awk represents the number of fields with an integer enclosed in parentheses.

NR variable

In addition, in addition to counting the number of fields in each record, awk also counts the number of input records. The number of records is stored in the variable NR, which is used in the same way as other variables. For example, to display the line number at the beginning of each line:

$awk'{print NR ":" $0} 'colours.txt1: name color amount2: apple red 43: banana yellow 64: raspberry red 35: grape purple 10 [...]

Note that you can write this command without adding spaces between the arguments after print, although this reduces readability:

$awk'{print NR ":" $0} 'colours.txtprintf () function

To make the format of the output more flexible, you can use awk's printf () function. It is similar to printf in C, Lua, Bash, and other languages. It also accepts format parameters separated by commas. The parameter list needs to be written in parentheses.

$printf format, project 1, project 2,...

Format is a parameter (also known as a formatter) that defines how other parameters are displayed. This function is implemented with format modifiers. % s outputs characters and% d outputs decimal digits. The following printf statement shows the number of fields in parentheses:

$awk 'printf "% s (% d)\ n", $0dl NF}' colours.txtname color amount (3) raspberry red 4 (3) banana yellow 6 (3) [...]

In this example,% s (% d) determines the output format of each line, and $0 Magi NF defines the data inserted in% s and% d positions. Note that, unlike the print function, the output does not go to the next line when there is no explicit instruction. The line breaks only when the escape character\ nappears.

Awk script programming

All the awk code that appears in this article has been executed in the Bash terminal. In the face of more complex programs, it is easier to put commands in files (scripts). The-f FILE option (not to be confused with-F, which is used for field delimiters) can be used to indicate the file that contains the executable program.

For example, here is a simple awk script. Create a file called example1.awk that contains the following:

/ ^ a / {print "A:" $0} / ^ b / {print "B:" $0}

If a file contains an awk program, it is best to write the .awk extension when naming the file. This naming is not mandatory, but doing so will give the file manager, editor (and you) a useful hint about the contents of the file.

Execute this script:

$awk-f example1.awk colours.txtA: raspberry red 4B: banana yellow 6A: apple green 8

A file containing the awk command, with the companion #! at the beginning of the line, becomes an executable script. Create a file called example2.awk that contains the following:

#! / usr/bin/awk-fallow # except for the first line, the line number # NR > 1 {printf "% d:% s\ n", NR,$0} is displayed before the other lines

It can be said that there is only one line in the script, which is useless in most cases. But in some cases, it's much easier to execute a script than to remember and then type a command. A script file also provides a good opportunity to record the specific role of the command. Lines that begin with the # sign are comments, and awk ignores them.

Give executable permissions to the file:

Chmod Ubunx example2.awk execute script: $. / example2.awk colours.txt2: apple red 42: banana yellow 64: raspberry red 35: grape purple 10 [...]

One advantage of putting the awk command in a script file is that it is easier to modify and format the output. In a terminal, if you can execute multiple awk commands in one line, it would be superfluous to enter multiple lines to achieve the same effect.

Try it

You now know enough about how awk executes instructions. Now you should be able to write complex awk programs. Try to write an awk script that includes at least one conditional pattern and multiple rules. If you want to use functions other than print and printf, you can refer to the online gawk manual.

The following example is a good starting point:

#! / usr/bin/awk-pi # shows all records except for the following # if the first record contains "raspberry" # replace "red" with "pi" $1 = = "raspberry" {gsub (/ red/, "pi")} {print}

Try to execute the script and see what the output is. It's up to you next.

This is the end of the article on "how to use fields, records and variables in the awk command". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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