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

Awk command (2)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

1. Regular expression of awk

1. Common uses of regular expressions

[Amurz] A capital letter

[aMuz] one lowercase letter

[0-9] A number

[^ Amurz] A character other than uppercase letters

[^ a-zA-Z] A character other than an English alphabet

[^ a-zA-Z0-9] A non-alphabetic and non-numeric character

[amurz]\ {3jue 5\} represents a string of lowercase letters with a length of 3'5.

two。 View tcp port statu

[root@localhost ~] # netstat-ltn

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

Tcp 0 0 127.0.0.1 2208 0.0.0.0 * LISTEN

Tcp 0 0 0.0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 01. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 15. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0

Tcp 0 0 0.0.0.0 817 0.0.0.015 * LISTEN

Tcp 0 0 127.0.0.1 631 0.0.0.0 * LISTEN

Tcp 0 0 127.0.0.1 25 0.0.0. 0 LISTEN

Tcp 0 0 127.0.0.1 2207 0.0.0.0 * LISTEN

Tcp 0 0: 5989: * LISTEN

Tcp 0 0: 22:: * LISTEN

3. Intercept port information

[root@localhost ~] # netstat-ltn | awk'{print $4}'| awk-F:'/ [0-9] $/ {print $NF}'

2208

one hundred and eleven

eight hundred and seventeen

six hundred and thirty one

twenty-five

2207

5989

twenty-two

4. Print data that match mysql or oracle

[root@localhost ~] # awk'$0~/mysql | oracle/ {print $0}'/ etc/passwd

Mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

Oracle:x:500:501::/home/oracle:/bin/bash

5. Print data that match m or o or r

[root@localhost ~] # awk'$0000 / ^ [mor] / {print $0}'/ etc/passwd

Root:x:0:0:root:/root:/bin/bash

Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

Operator:x:11:0:operator:/root:/sbin/nologin

Mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

Mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

Oracle:x:500:501::/home/oracle:/bin/bash

6. Print non-alphabetic data

[root@localhost ~] # awk'{if ($0,000,^ [^ a-zA-Z] /) print $0}'/ etc/passwd

II. Simple use of awk variables, judgments and loops

1, define variable a = "name is:", print variable an and first column

[root@localhost ~] # awk-F:'{a = "name is:"} / root/ {print a recording 1}'/ etc/passwd

Name is: root

Name is: operator

2.begin defines the variable, defines the variable aa=0, adds 1 to match root, and prints the command aa after completion

[root@localhost ~] # awk 'BEGIN {aa=0} / root/ {aa++} END {print aa}' / etc/passwd

two

3.if judgment

[root@localhost ~] # awk-F:'{if ($7 million ~ "nologin") print $1

Root / bin/bash

2 / sbin/nologin

Sync / bin/sync

Shutdown / sbin/shutdown

9 / sbin/nologin

32 / sbin/nologin

Mysql / bin/bash

Oracle / bin/bash

...

4. Cycle

There are two lines in if view

[root@localhost ~] # awk'{if ($0~/root/) print $0}'/ etc/passwd

Root:x:0:0:root:/root:/bin/bash

Operator:x:11:0:operator:/root:/sbin/nologin

While quits after querying only one row

[root@localhost ~] # awk'{while ($0~/root/) {print $0bot exit}'/ etc/passwd

Root:x:0:0:root:/root:/bin/bash

III. & & and | |

[root@localhost shell] # tail-3 / etc/passwd | awk-F:'{if ($0 ~ "27" & & $0 ~ "bash") print $0}'

Mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

[root@localhost shell] # tail-3 / etc/passwd | awk-F:'{if ($0 ~ "mysql" | | $0 ~ "oracle") print $0}'

Mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

Oracle:x:500:501::/home/oracle:/bin/bash

4. Awk built-in variables

RS: line delimiter

FS: column delimiter

OFS: define column delimiters

ORS: defines the line delimiter. The default is\ nWrap

FILENAME: file name

FNR: when awk processes multiple files, the awk NR variable gives the current record number (line number) for all files, and Awk FNR displays the current line number for each file.

RS record line delimiter, the default delimiter is\ n.

[root@localhost ~] # echo "1 awk BEGIN {RS="-- "} {print}'

one

two

three

The ORS record output delimiter. The default value is\ n. ORS can be regarded as the reverse process of RS.

[root@localhost ~] # seq 3 | awk 'ORS=NR%3? "- -": "\ n" {print}'

1Mui Mui 2 Mui Mui 3

[root@localhost ~] # seq 3 | awk'{ORS= "-"; print}'; echo

1 Mui 2 Mui Mui 3 Mui Mui-

Replace-with *, FS and OFS replace each other

[root@localhost ~] # echo "1 awk-F BEGIN-'BEGIN {OFS=" *} {print $1 meme 2meme 3}'

1, 2, 2, 3.

The usage of FILENAME

[root@localhost shell] # awk 'BEGIN {FS= ":"} {print NF,NR,$NF,$0} END {print "= =\ n" FILENAME}' / etc/passwd

1 1 root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash

1 2 bin:x:1:1:bin:/bin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin

1 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin

1 4 adm:x:3:4:adm:/var/adm:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin

....

1 40 oracle:x:500:501::/home/oracle:/bin/bash oracle:x:500:501::/home/oracle:/bin/bash

=

/ etc/passwd

5. Awk built-in string function

1.gsub (rpenery s) replaces r with s in the whole $0

[root@localhost ~] # awk 'gsub ("root", "bob") {print $0}' / etc/passwd

Bob:x:0:0:bob:/bob:/bin/bash

Operator:x:11:0:operator:/bob:/sbin/nologin

The effect is equivalent to the global replacement of sed

[root@localhost ~] # sed-n's etc/passwd

Bob:x:0:0:bob:/bob:/bin/bash

Operator:x:11:0:operator:/bob:/sbin/nologin

Gsub replace r with s in the whole t

[root@localhost ~] # awk 'gsub ("root", "bob", $2) {print $0}' sedtest.txt

Root bob root

Root bob root

2.sub replace s with the leftmost and longest substring of $0

[root@localhost ~] # awk 'sub ("root", "bob") {print $0}' / etc/passwd

Bob:x:0:0:root:/root:/bin/bash

Operator:x:11:0:operator:/bob:/sbin/nologin

The effect is equivalent to sed local replacement.

[root@localhost ~] # sed-n's etc/passwd

Bob:x:0:0:root:/root:/bin/bash

Operator:x:11:0:operator:/bob:/sbin/nologin

3.index (spendium t) returns the position of the string t in s

[root@localhost ~] # awk 'BEGIN {print index ("I love you", "ov")}'

four

4.length (s) returns s length

[root@localhost ~] # awk 'BEGIN {print length ("I love you")}'

ten

5.match (SCHI r) tests whether s contains a string that matches r

[root@localhost ~] # awk 'BEGIN {print match ("I love you", "v")}'

five

[root@localhost ~] # awk 'BEGIN {print match ("I love you", "a")}'

0

6.split divides s into sequence an on fs.

7.substr (sjournal p) returns the suffix part of the string s that begins with p

8.substr returns the suffix part of the string s whose length is n starting from p

VI. Awk built-in mathematical functions

Int (x)

Sqrt (x)

Exp (x)

Log (x)

Sin (x)

Cos (x)

Etc.

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

Database

  • J2ee distributed architecture dubbo + springmvc + mybatis + ehcache + redis distributed architecture

    Introduce

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

    12
    Report