In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use awk in linux. It is very detailed and has a certain reference value. Friends who are interested must finish it!
1. Intercept a segment in a document
The example commands are as follows:
# head-N2 test.txt | awk-F':'{print $1} 'rootbin
In this case, the-F option specifies the delimiter. If the-F option is not added, spaces or tab are used as delimiters. Print is the act of printing, used to print a field. $1 is the 1st field, $2 is the 2nd field, and so on. However, $0 is special in that it represents the entire line:
# head-N2 test.txt | awk-favored root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin'{print $0} 'root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin
Note the format of awk.-F is followed by single quotation marks, which are delimited. The actions of print should be enclosed in {}, otherwise an error will be reported. Print can also print customized content, but the customized content should be enclosed in double quotes, as shown below:
# head-N2 test.txt | awk-F':'{print $1 "#" $2 "#" $3 "#" $4} 'root#x#0#0bin#x#1#12. Match character or string
Filter out the lines containing the specified string in the document. The example command is as follows:
# awk'/ oo/' test.txtroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsetroubleshoot:x:992:990::/var/lib/setroubleshoot:/sbin/nologin
This is similar to the use of sed, can achieve the function of grep, but there is no color display, certainly not as easy to use as grep. However, awk has a more powerful match than sed, as shown below:
# awk-F':'$1 ~ / oo/' test.txtroot:x:0:0:root:/root:/bin/bashsetroubleshoot:x:992:990::/var/lib/setroubleshoot:/sbin/nologin
It can let a certain segment to match, here ~ is the meaning of matching.
In addition, awk can match multiple times, as shown below:
# awk-F':'/ root/ {print $1 test.txtroot 0operator 11test 3} / test/ {print $1 camera 3} 'test.txtroot 0operator 11test 1006
In this case, the awk matches the root, then the test, and prints only the matched segments.
3. Conditional operator
Sometimes, qualifying conditions are given when matching characters, such as 0 in paragraph 3. The example commands are as follows:
# awk-F':'$3 million = "0" / etc/passwdroot:x:0:0:root:/root:/bin/bash
In awk, we can use logical symbols to judge, for example, = = equals, or it can be understood as an exact match. In addition, there are >, > =,
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.