In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use awk grammar in Linux". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Introduction to awk and expression examples
A language with a strange name
Mode scanning and processing, processing data and generating reports.
Awk is not only a command in the linux system, but also a programming language; it can be used to process data and generate reports (excel); the data processed can be one or more files; it can be directly from the standard input, or it can be obtained from the standard input through the pipeline; awk can directly edit commands on the command line for operation, or it can be written into awk programs for more complex applications.
Sed handles stream editor text streams, streams.
I. brief introduction of awk environment
The awk covered in this article is gawk, that is, the GNU version of awk.
[root@creditease awk] # cat / etc/redhat-releaseCentOS Linux release 7.5.1804 (Core) [root@creditease awk] # uname-r3.10.0-862.el7.x86_64 [root@creditease awk] # ll `which awk`lrwxrwxrwxrwx. 1 root root 4 Nov 7 14:47 / usr/bin/awk-> gawk [root@creditease awk] # awk-- versionGNU Awk 4.0.2 II. Format of awk
Awk instructions are made up of patterns, actions, or a combination of patterns and actions.
The pattern, or pattern, can be similar to the pattern matching of sed, which can be composed of expressions or regular expressions between two forward slashes. For example, NR==1, which is a pattern, can be understood as a condition.
An action, or action, consists of one or more statements within curly braces, separated by semicolons. The following awk uses the format.
Record and domain name meaning record record, row filed domain, region, field, column
1) NF (number of field) represents the number of regions (columns) in a row, and $NF takes the last region.
2) the $symbol indicates that a column (region) is taken, and $1 is "2" NF.
3) NR (number of record) line number. Awk has a built-in variable NR to save the record number of each line, and the NR value of each record will be automatically + 1 after processing.
4) FS (- F) field separator column separator, what separates rows into multiple columns
3.1 specify the delimiter [root@creditease awk] # awk-F "#" {print $NF} 'awk.txt GKL$123GKL$213GKL$321 [root@creditease awk] # awk-F' [# $]'{print $NF} 'awk.txt 1232133213.2 basic conditions and actions [root@creditease awk] # cat awk.txt ABC#DEF#GHI#GKL$123BAC#DEF#GHI#GKL$213CBA#DEF#GHI#GKL$321 [root@creditease awk] # awk-F "#"' NR==1 { Print $1} 'awk.txtABC3.3 only condition [root@creditease awk] # awk-F "#"' NR==1' awk.txtABC#DEF#GHI#GKL$123
There is an action {print $0} by default.
Only action [root@creditease awk] # awk-F "#"'{print $1} 'awk.txtABCBACCAB
All rows are processed by default
3.5 multiple modes and actions [root@creditease awk] # awk-F "#" NR==1 {print $NF} NR==3 {print $NF} 'awk.txt GKL$123GKL$3213.6 's understanding of $0
$0 in awk represents the entire line
[root@creditease awk] # awk'{print $0} 'awk_space.txtABC DEF GHI GKL$123BAC DEF GHI GKL$213CBA DEF GHI GKL$3213.7 FNR
FNR is similar to NR, except that multi-file records are not incremented, and each file starts at 1 (more on dealing with multiple files later).
[root@creditease awk] # awk'{print NR} 'awk.txt awk_space.txt 123456 [root@creditease awk] # awk' {print FNR} 'awk.txt awk_space.txt 123123 IV. Regular expressions and operators
Awk, like sed, can match input text through pattern matching. Awk also supports a large number of regular expression patterns, most of which are similar to the metacharacters supported by sed, and regular expressions are a must-have tool for turning three Musketeers.
Regular expression metacharacters supported by awk
Metacharacters not supported by awk by default, and metacharacters that need to be supported by adding parameters
The metacharacter function example explains that x {m} x repeats m times / cool {5} / it should be noted that the difference between cool with or without parentheses, x can make the string just one character, so / cool {5} / means matching coo plus five l, that is, coolllll. / (cool) {2,} / indicates matching coolcool,coolcoolcool, etc. X {m,} x repeat at least m times / (cool) {2,} / ditto x {mrecogine n} x repeat at least m times, but not more than n times, you need to specify the parameter:-- posix or-- re-interval. This mode / (cool) {5pc6} / ditto cannot be used without this parameter
In the application of regular expressions, the default is to find a matching string within the line, and action is performed if there is a match, but sometimes only a fixed list is needed to match the specified regular expression.
For example:
I want to take the fifth column ($5) in the / etc/passwd file to find the line that matches the mail string, so I need to use the other two matching operators. And there are only these two operators in awk to match regular expressions.
The regular matching operator ~ is used to match the expression of a record or region.! Used to express the opposite meaning. 4.1 regular instance
1) display the GHI column in awk.txt
[root@creditease awk] # cat awk.txt ABC#DEF#GHI#GKL$123BAC#DEF#GHI#GKL$213CBA#DEF#GHI#GKL$321 [root@creditease awk] # awk-F "#" {print $3} 'awk.txt GHIGHIGHI [root@creditease awk] # awk-F "#" {print $(NF-1)}' awk.txt GHIGHIGHI
2) display rows containing 321
[root@creditease awk] # awk'/ 321 / {print $0} 'awk.txt CBA#DEF#GHI#GKL$321
3) use # as the delimiter to display the rows where the first column begins with B or the last column ends with 1
[root@creditease awk] # awk-F "#" $1bn / ^ B / {print $0} $NF~/1 $/ {print $0} 'awk.txt BAC#DEF#GHI#GKL$213CBA#DEF#GHI#GKL$321
4) display the lines of the first column starting with B or C with the # delimiter
[root@creditease awk] # awk-F "#" $1bn / ^ B | ^ C / {print $0} 'awk.txt BAC#DEF#GHI#GKL$213CBA#DEF#GHI#GKL$321 [root@creditease awk] # awk-F "#" $1bn / ^ [BC] / {print $0}' awk.txt BAC#DEF#GHI#GKL$213CBA#DEF#GHI#GKL$321 [root@creditease awk] # awk-F "#" $1bn / ^ (B | C) / {print $0} 'awk.txt BAC#DEF#GHI#GKL$213CBA # DEF#GHI#GKL$321 [root@creditease awk] # awk-F "#"'$1clients / ^ A / {print $0} 'awk.txt BAC#DEF#GHI#GKL$213CBA#DEF#GHI#GKL$ 3215, Comparative expression
Awk is a programming language that can make more complex judgments. When the condition is true, awk executes the relevant action, mainly to make relevant judgments for a certain area, such as those whose printing scores are above 80 points, so it is necessary to make a comparative judgment on this area.
The following table lists the relational operators that awk can use to compare numeric strings and regular expressions. When the expression is true, the result of the expression is 1, otherwise it is 0. Awk executes the relevant action only if the expression is true.
Relational operators supported by awk
Operator meaning example: y > greater than x "xin.txt"} 3. Matters needing attention in awk
A) NR==FNR # # cannot be written as NR=FNR (= means assignment in awk)
B) non-linear FNR # # NR does not equal FNR
C) {axi1smara [NR]} this will result in an error: variables and array names in the same command cannot be repeated d) do not wrap when printf is output
E) {print}, or print in parentheses can be directly redirected to a new file, the file name enclosed in double quotation marks. For example: {print $1 > "xin.txt"}
F) when the mode (condition) is 0, the following action is not performed. The latter action is not performed until 0.
This is the end of the content of "how to use awk grammar in Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.