Shell Primer-awk-1

Source: Internet
Author: User

The advantage of awk for sed grep is segmentation, dividing the delimiter into a small segment, matching, replacing, manipulating, logical judging, comparing ... such as

Basic operations of AWK

[[email protected] ~]# awk-f ': ' {print $} ' 1.txt////-F Specify delimiter $ is the third paragraph
0
1
2
3

.....

Specify multiple segments

[[email protected] ~]# awk-f ': ' {print $3,$4,$1} ' 1.txt
0 0 Root
1 1 Bin
2 2 Daemon
3 4 ADM
4 7 LP

.....

Specifies that the specified delimiter is displayed

[[email protected] ~]# awk-f ":" ' ofs= ":" {print $3,$4,$1} ' 1.txt
0:0:root
1:1:bin
2:2:daemon
3:4:adm

......

Match specified characters

[Email protected] ~]# awk '/root/' 1.txt
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin

Match multiple characters

[Email protected] ~]# awk '/root|user/' 1.txt
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
saslauth:x:499:76: "SASLAUTHD user":/var/empty/saslauth:/sbin/nologin

with special characters.

[Email protected] ~]# awk '/r*o/' 1.txt
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin

...

Segment Matching

[[email protected] ~]# awk-f ': ' $1~/r*o/' 1.txt////search for the first line with R*o with a colon delimiter
Root:x:0:0:root:/root:/bin/bash
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

.....

Segment matches and displays only the specified rows

[[email protected] ~]# awk-f ': ' $1~/r*o/{print $} ' 1.txt///The third paragraph of the line showing the first paragraph R*o
0
2
6
11

....

Match two keywords simultaneously and specify the specified segment of the line now

[Email protected] ~]# awk-f ': ' $1~/r*o/{print $1,$3}; $1~/user/{print $1,$3} ' 1.txt

[[email protected] ~]# awk-f ': ' $1~/r*o|user/{print $1,$3} ' 1.txt

Root 0
Daemon 2
Shutdown 6
Operator 11

.....

//////////////////////////////////////////////////////////////////////////////////////////

Summary: awk-f Specifies the delimiter ': ' ofs= ': ' Specifies that the separator that displays the result matches the '/r*o/' specified filter condition {print $} displays the specified file for the filter result

Awk-f ': ' $1~/r*o/' is specified according to the paragraph to search ~ followed by keywords

| You can combine searches;

Shell Primer-awk-1

Related Article

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.