In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what is the basic format of awk in linux", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn about "what is the basic format of awk in linux".
This paper introduces the basic format of 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 treats 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 upright x example2.awk
Execute the 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.
The above is all the content of the article "what is the basic format of awk in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.