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

What is awk? How does awk work? How to use awk?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

1. Introduction of awk

1. Awk Overview awk is a = = programming language = =, mainly used to deal with = = text and data = = under linux/unix, is a tool under linux/unix. Data can come from standard input, one or more files, or the output of other commands. The way awk handles text and data: = = progressive scan = = file, by default, from the first line to the last line, look for matching lines of = = specific pattern = =, and do what you want on these lines. Awk represents the first letter of the author's last name, respectively. Because it is written by three people, namely, Alfred Aho, Brian Kernighan and Peter Weinberger.

Gawk is the GNU version of awk and provides some extensions to Bell Labs and GNU.

The awk described below takes the gawk of GNU as an example. Awk has been linked to gawk in the linux system, so the following is all introduced in awk. 2. What can awk do? awk== is used to deal with files and data = =. It is a tool under the class unix, and it is also a programming language that can be used to judge the supporting conditions, such as the number of visits to the website, the amount of IP visited, and so on. Support for and while loop 2, awk usage mode 1. = command line mode use = 1) grammatical structure awk option 'command part' file name special description: references to shell variables need to be enclosed in double quotation marks 2) Common options introduction = =-fission = define field division symbols, the default delimiter is = = space = =-v defines variables and assigns values 3) ='= naming part description ='= = regular expression Address location'/ root/ {awk statement} 'sed:' / root/p''NR==1,NR==5 {awk statement} 'sed:' 1authoring 5p awk statement / ^ root/,/ ^ ftp/ {awk statement} 'sed:' / ^ root/,/ ^ ftp/p' {awk statement 1 = = = awk statement 2 "awk statement".}'{print $0: print $1} 'sed:' p''NR==5 {print $0} 'sed:' 5p 'Note: awk command statements are separated by semicolons BEGIN...END....'BEGIN {awk statement}; {processing}; END {awk statement}' 'BEGIN {awk statement}; {processing}' {processing}; END {awk statement}'2. Script mode uses 1) scripting #! / bin/awk-f defines magic characters the following is a list of commands in awk quotation marks, do not protect commands with quotation marks Multiple commands are separated by semicolons BEGIN {FS= ":"} NR==1 NR==3 {print $1 "\ t" $NF}... 2) script execution method 1:awk option-text file of f awk script file to process awk-f awk.sh filenamesed-f sed.sh-I filename method 2:./awk script file (or absolute path) text file to be processed. / awk.sh filename./sed.sh filename III, awk internal related variable description remarks = = $0 = all records of the current processing line = =\ $1 Awk-F:'{print\ $1,\ $3}'= = number of fields in the current record of NF== (number of columns) awk-F:'{print NF}'= = $NF== last column $(NF-1) represents the penultimate column = = FNR/NR== line number = = FS== definition spacer 'BEGIN {FS= ":" {print\ $1 RS= 3}'= OFS== defines the output field delimiter, = = default space = = 'BEGIN {ORS= "\ t"}; print\ $1 print 3}' RS input record separator, default newline 'RS= "\ t}; {print $0}' ORS output record separator, default newline 'BEGIN {ORS="\ n\ n "} {print\ $1 the file name currently entered by 1.txt# awk 3} 'FILENAME, = = examples of commonly used built-in variables = # awk-F:' {print $1 minus $(NF-1)} '1.txt# awk-F:' {print $1 1.txt# awk'/ root/ {print $0} '1.txt# awk' / root/' 1.txt# awk-F:'/ root/ {print $1 $NF} '1.txt root/ bin/bash# awk-F:' / root/ {print $0} '1.txt root:x:0:0:root:/root:/bin/bash# awk' NR==1,NR==5' 1.txt# awk 'NR==1,NR==5 {print $0}' 1.txt# awk 'NR==1,NR==5 / ^ root/ {print $0} '1.txt root:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin2, Built-in variable separator examples FS and OFS:# awk 'BEGIN {FS= ":"} / ^ root/,/ ^ lp/ {print $1 sbin/nologinadm NF} '1.txt# awk-F:' BEGIN {OFS= "\ t\ t"}; / ^ root/,/ ^ lp/ {print $1 camera NF} '1.txt root/ bin/bashbin / sbin/nologindaemon / sbin/nologinadm / sbin/nologinlp / sbin/nologin# awk-F:' BEGIN {OFS= "@"} / ^ root/,/ ^ lp/ {print $1 $NF} '1.txtroot @ / bin/bashbin@@@/sbin/nologindaemon@@@/sbin/nologinadm@@@/sbin/nologinlp@@@/sbin/nologin [root@server shell07] # RS and ORS: modify the first two lines of the source file by adding tabs and contents: vim 1.txtroot:x:0:0:root:/root:/bin/bash hello worldbin:x:1:1:bin:/bin:/sbin/nologin test1 test2# awk' BEGIN {RS= "\ t"} {print $0} '1.txt# awk' BEGIN {ORS= "\ t"}; {print $0} '1.txt IV. Awk working principle

Awk-F:'{print $1J'3}'/ etc/passwd

Awk takes a line as input and assigns this line to the internal variable $0. Each line can also be called a record, ending with a RS character

Each line is broken down into fields (or fields) by the spacer =: = (default is a space or tab), and each field is stored in a numbered variable, starting with $1

Q: how does awk know to separate fields with spaces?

Answer: because there is an internal variable = = FS== to determine the field delimiter. Initially, FS is assigned a space

Awk uses the print function to print fields, which are separated by = = spaces, because there is a comma between\ $1 and\ $3. The comma is special because it maps to another internal variable called the = = output field delimiter = = OFS, which defaults to a space

After awk processes one line, it takes another line from the file, stores it in $0, overwrites the original content, and then separates the new string into fields and processes it. The process continues until all lines have been processed

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

Servers

Wechat

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

12
Report