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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "detailed introduction of linux regular expression awk", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "linux regular expression awk detailed introduction"!
Awk, like sed, is a streaming editor. It also operates on lines in the document, one line at a time. Awk is more powerful than sed, it can do what sed can do, but also can do what sed can not do. Awk is often used to segment
Awk can implement +? * without adding any parameters. | | these special symbols |
1. Intercept a segment in a document
[root@yonglinux ~] # head-N2 passwd | awk-F:'{print $1} 'rootbin [root@yonglinux ~] # head-N2 passwd | awk-F:' {print $0} 'root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin [root@yonglinux ~] # head-N2 passwd | awk-F:' {print $1, 3, 7} root 0 / bin/bashbin 1 / sbin/nologin
The-F option specifies the delimiter, or if not specified by-F, with a space or tab as the delimiter. Print is the act of printing, used to print out a field. $1 is the first field, $2 is the second field, and so on, and one special one is $0, which represents the entire line.
You can print multiple fields within {} $1, 3, 7, separated by commas.
The default delimiter for printing segments is a space. You can customize the delimiter, which needs to be enclosed in double quotes; you can also define the output delimiter by OFS
[root@localhost ~] # awk-F:'{print $3 awk} '1.txt | head-50 01 12 23 44 7 [root@localhost ~] # awk-F:' {print $3 ":" $4} '1.txt | head-50 root@localhost 0112 awk 447 [root@localhost ~] # awk-F:' OFS= "#" {print $3 " $4} '1.txt | head-5001122344007 [root@yonglinux ~] # head-N2 passwd | awk-F:' {print $1 "#" @ "$3" # "$7} 'root#@0#/bin/bashbin#@1#/sbin/nologin
Pay attention to the format of awk,-F followed by single quotation marks, and then a delimiter inside, print actions should be enclosed in {}, otherwise an error will be reported. Print can also print custom content, but the custom content should be enclosed in "" double quotes.
2. Match character or string
[root@yonglinux ~] # awk-F:'$1mm awk passwd user1:x:600:501::/home/user1:/bin/bash 'passwd games:x:12:100:games:/usr/games:/sbin/nologin [root@yonglinux ~] # awk-F:' $1mm passwd user1:x:600:501::/home/user1:/bin/bash'
You can let a segment match, ~ indicates the meaning of match, separates the first field with a colon and then matches the keywords in / /
[root@yonglinux ~] # awk-F:'/ root/ {print $1 passwd root 0operator 11ftp 14saslauth 499user1 3} / user/ {print $1 camera 3} 'passwd root 0operator 11ftp 14saslauth 499user1
Awk can also be matched multiple times, as in the above example, the full text matches the line containing the root keyword, then matches the line containing user, and prints the first and third paragraphs of the match.
3. Conditional operator
Determine that the third field is 0
[
Root@yonglinux ~] # awk-F:'$3 million = "0" 'passwd root:x:0:0:root:/root:/bin/bash [root@yonglinux ~] # awk-F:' $3 million 10 'passwd uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Determine that the third field is 10 and print the seventh field of the line
[root@yonglinux ~] # awk-F:'$3 million 10 {print $7} 'passwd / sbin/nologin [root@yonglinux ~] # awk-F:' $3 million = "600" 'passwd user1:x:600:501::/home/user1:/bin/bash
In awk, it can be judged by logical symbols, for example,'= ='is equal to, and it can also be understood as' exact match 'and there are also >,' > =,'
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.