12. sed command and regular expression

Source: Internet
Author: User

awk command

To intercept a segment in a document

#awk-F ': ' {print$1} 1.txt prints the first paragraph

#awk-F ': ' ofs= ' # ' {print$1,$2,$3,$4} 1.txt specifies the delimiter to display the result as #, or you can use a custom character to concatenate each segment {print$1 "#"

Match character or string

#awk '/root/' 1.txt

Match for a segment

#awk-F ': ' $ ~/oo/' 1.txt

Multiple matches

#awk-F ': '/root/{print $1,$3};$1~/test/;$3~/20/' 1.txt

Change the value of a segment

#awk-F ': ' $1= ' root ' 1.txt

Number calculation, add the third and fourth values, and give the seventh paragraph

#awk-F ' {ofs= ': '} {$7=$3+$4;print$0} ' 1.txt

Calculate the sum of the third paragraph

#awk-F ': ' {(tot=tot+$3)}; End{print tot} ' 1.txt

if keyword

#awk-F ': ' {if ($1== "root") print$0} ' 1.txt

Built-in variable NF number, NR line number '

Print lines after 20 lines

#awk ' nr>20 ' 1.txt

Print 20 lines later and the first paragraph contains ' SSH ' lines

#awk-F ': ' nr>20 && $1~/ssh/' 1.txt


Conditional operator <,>,==,!=,>=,<=

The third paragraph is 0

#awk-F ': ' $3== ' 0 "' 1.txt


The third paragraph is greater than or equal to 500

#awk-F ': ' $3>=500 ' 1.txt

Description, and so on when comparing numbers, cannot add double quotes, if written $3>= "500" does not meet the requirements

The 7th paragraph is not '/sbin/nologin '

#awk-F ': ' $7!= '/sbin/nologin ' 1.txt

The third paragraph is less than the fourth paragraph

#awk-F ': ' $3<$4 ' 1.txt

The third segment is greater than 5 and less than 7

#awk-F ': ' $3>5 && $3<7 ' 1.txt

The third paragraph is greater than 5, or the 7th paragraph is '/bin/bash '

#awk-F ': ' $3> "5" | | $7== "/bin/bash" ' 1.txt


Extended
Using external shell variables in awk http://www.aminglinux.com/bbs/thread-199-1-1.html
awk merges a file http://www.aminglinux.com/bbs/thread-493-1-1.html
Concatenate a file multiple lines into a line http://www.aminglinux.com/bbs/thread-266-1-1.html
The use of Gsub functions in awk http://www.aminglinux.com/bbs/thread-200-1-1.html
awk intercepts specifying multiple fields as one row http://www.aminglinux.com/bbs/thread-224-1-1.html
Filter two or more keywords http://www.aminglinux.com/bbs/thread-198-1-1.html
Generate the following structure file with awk http://www.aminglinux.com/bbs/thread-5494-1-1.html
awk Tutorial Http://www.cnblogs.com/emanlee/p/3327576.html

awk Prints single quotation marks with print http://www.aminglinux.com/bbs/thread-1738-1-1.html

Merge two files http://www.aminglinux.com/bbs/thread-945-1-1.html

650) this.width=650, "src=" Http://www.test.com/static/image/common/logo_88_31.gif "width=" "height=" "alt=" logo _88_31.gif "/>

12. sed command and regular expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.