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

Detailed explanation of Linux awk command

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

AWK is a language that processes text files and is a powerful text analysis tool. It is called AWK because it takes the first characters of the three founders Alfred Aho,Peter Weinberger and Brian Kernighan's Family Name.

Awk is a powerful text analysis tool, compared with the search of grep and the editor of sed, awk is particularly powerful in analyzing data and generating reports. To put it simply, awk is to read the file line by line, slice each line with a space as the default separator, and then analyze the cut part.

Usage: awk'{pattern + action}'{filenames}

Awk built-in variables:

The nth field of the current record, separated by FS

$0 complete input record

Number of ARGC command line arguments

The location of the current file on the ARGIND command line (starting at 0)

ARGV contains an array of command line arguments

CONVFMT digital conversion format (default is% .6g) ENVIRON environment variable associative array

Description of the last system error in ERRNO

List of FIELDWIDTHS field widths (separated by spacebar)

FILENAME current file name

Line numbers counted separately for each file in FNR

FS sets the input field separator, which is equivalent to the command line-F option

If IGNORECASE is true, a match that ignores case is performed

NF the number of fields in a record

The number of records that NR has read out, which is the line number, starting with 1

Output format of OFMT numbers (default is% .6g)

OFS output record delimiter (output newline character), which is output with the specified symbol instead of the newline character

ORS output record delimiter (default is a newline character)

RLENGTH the length of the string matched by the match function

RS record delimiter (default is a newline character)

RSTART the first position of the string matched by the match function

SUBSEP array subscript delimiter (default is / 034)

$NF is number finally, which represents the information in the last column, which is different from the variable NF, which counts the total number of each row and column.

Format format:

% c: display a single character

% dline% I: decimal integer

% eForce% E: display value by scientific counting method

% f: show floating point number

% gpeng% G: displays values in scientific counting format or floating point format

% s: display string

% u: unsigned integer

%%: show% itself

Modifier:

N: display width, N is a number

-: left alignment, default is right alignment

+: display numeric symbol

\ t Tab character

\ nLine feed character

~ match, which is not an exact comparison compared with =

! ~ mismatch, imprecise comparison

= = equal to, must be all equal, accurate comparison

! = not equal to exact comparison

& & Logic and

| | Logic or

+ 1 or more when matching

/ [0-9] [0-9] + / two or more digits

/ [0-9] [0-9] * / one or more numbers

Awk column output:

1. Search / etc/passwd all lines with the root keyword

[root@127-0-0-1 scripts] # awk'/ root/' / etc/passwd

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

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

two。 Search / etc/passwd for all rows with the root keyword and output the shell column

[root@127-0-0-1 scripts] # awk-F ":"'/ root/ {print $7}'/ etc/passwd

/ bin/bash

/ sbin/nologin

3. Statistics / etc/passwd: file name, line number of each line, number of columns per line, corresponding full line content

[root@127-0-0-1 scripts] # awk-F ":"'{print "filename:" FILENAME ", linenumber:" NR ", columns:" NF ", linecontent:" $0}'/ etc/passwd

Filename:/etc/passwd,linenumber:1,columns:7,linecontent:root:x:0:0:root:/root:/bin/bash

Filename:/etc/passwd,linenumber:2,columns:7,linecontent:bin:x:1:1:bin:/bin:/sbin/nologin

Filename:/etc/passwd,linenumber:3,columns:7,linecontent:daemon:x:2:2:daemon:/sbin:/sbin/nologin

Filename:/etc/passwd,linenumber:4,columns:7,linecontent:adm:x:3:4:adm:/var/adm:/sbin/nologin

Filename:/etc/passwd,linenumber:5,columns:7,linecontent:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Filename:/etc/passwd,linenumber:6,columns:7,linecontent:sync:x:5:0:sync:/sbin:/bin/sync

Filename:/etc/passwd,linenumber:7,columns:7,linecontent:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Filename:/etc/passwd,linenumber:8,columns:7,linecontent:halt:x:7:0:halt:/sbin:/sbin/halt

Filename:/etc/passwd,linenumber:9,columns:7,linecontent:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

Filename:/etc/passwd,linenumber:10,columns:7,linecontent:uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

Filename:/etc/passwd,linenumber:11,columns:7,linecontent:operator:x:11:0:operator:/root:/sbin/nologin

Filename:/etc/passwd,linenumber:12,columns:7,linecontent:games:x:12:100:games:/usr/games:/sbin/nologin

Filename:/etc/passwd,linenumber:13,columns:7,linecontent:gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

Filename:/etc/passwd,linenumber:14,columns:7,linecontent:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

Filename:/etc/passwd,linenumber:15,columns:7,linecontent:nobody:x:99:99:Nobody:/:/sbin/nologin

Filename:/etc/passwd,linenumber:16,columns:7,linecontent:dbus:x:81:81:System message bus:/:/sbin/nologin

Filename:/etc/passwd,linenumber:17,columns:7,linecontent:vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

Filename:/etc/passwd,linenumber:18,columns:7,linecontent:abrt:x:173:173::/etc/abrt:/sbin/nologin

Filename:/etc/passwd,linenumber:19,columns:7,linecontent:saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin

Filename:/etc/passwd,linenumber:20,columns:7,linecontent:postfix:x:89:89::/var/spool/postfix:/sbin/nologin

Filename:/etc/passwd,linenumber:21,columns:7,linecontent:haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

Filename:/etc/passwd,linenumber:22,columns:7,linecontent:ntp:x:38:38::/etc/ntp:/sbin/nologin

Filename:/etc/passwd,linenumber:23,columns:7,linecontent:sshd:x:74:74::/var/empty/sshd:/bin/false

Filename:/etc/passwd,linenumber:24,columns:7,linecontent:zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/bin/bash

Filename:/etc/passwd,linenumber:25,columns:7,linecontent:ntop:x:497:497:ntop:/var/lib/ntop:/sbin/nologin

Filename:/etc/passwd,linenumber:26,columns:7,linecontent:tcpdump:x:72:72::/:/sbin/nologin

Filename:/etc/passwd,linenumber:27,columns:7,linecontent:dev:x:500:500::/home/dev:/bin/bash

Filename:/etc/passwd,linenumber:28,columns:7,linecontent:ops:x:501:501::/home/ops:/bin/bash

Filename:/etc/passwd,linenumber:29,columns:7,linecontent:qa:x:502:502::/home/qa:/bin/bash

Filename:/etc/passwd,linenumber:30,columns:7,linecontent:bigdata:x:503:503::/home/bigdata:/bin/bash

Filename:/etc/passwd,linenumber:31,columns:7,linecontent:test:x:504:504::/home/test:/bin/bash

Filename:/etc/passwd,linenumber:32,columns:7,linecontent:autobots:x:505:505::/home/autobots:/bin/bash

Filename:/etc/passwd,linenumber:33,columns:7,linecontent:netdata:x:496:496:netdata:/opt/netdata:/sbin/nologin

Filename:/etc/passwd,linenumber:34,columns:7,linecontent:mysql:x:506:506:MySQL Server:/home/mysql:/bin/bash

Filename:/etc/passwd,linenumber:35,columns:7,linecontent:gluster:x:495:495:GlusterFS daemons:/var/run/gluster:/sbin/nologin

Filename:/etc/passwd,linenumber:36,columns:7,linecontent:apache:x:48:48:Apache:/var/www:/sbin/nologin

Filename:/etc/passwd,linenumber:37,columns:7,linecontent:mysql-proxy:x:494:494:MySQL-Proxy user:/:/sbin/nologin

4. Print the second line of information for / etc/passwd/

[root@127-0-0-1 scripts] # awk 'NR==2 {print "filename:" FILENAME,$0}' / etc/passwd

Filename:/etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin

5. Get information for the first column of rows 12 to 30

[root@127-0-0-1 scripts] # awk-F ":"'{if (NR > 12 & & NR)

< 30) print $1}' /etc/passwd gopher ftp nobody dbus vcsa abrt saslauth postfix haldaemon ntp sshd zabbix ntop tcpdump dev ops qa 6.多分隔符的使用,多个分隔符利用[]然后在里面写分隔符即可 [root@127-0-0-1 scripts]# cat yoon.txt 192.168.1.1:3307 [root@127-0-0-1 scripts]# awk -F '[.:]' '{print $1,$5}' yoon.txt 192 3307 7.添加了BEGIN和END hello world [root@127-0-0-1 scripts]# cat long.txt 2 this is a test 3 Are you like awk This's a test 10 There are orange,apple,mongo [root@127-0-0-1 scripts]# cat long.txt | awk 'BEGIN{print "name,shell"} {print $1,$NF} END {print "hello world"}' name,shell 2 test 3 awk This's test 10 orange,apple,mongo hello world 8.忽略大小写 [root@127-0-0-1 scripts]# awk 'BEGIN{IGNORECASE=1} /this/' long.txt 2 this is a test This's a test 9.使用正则,字符串匹配 输出第二列包含 "th",并打印第二列与第四列 [root@127-0-0-1 scripts]# awk '$2 ~ /th/ {print $2,$4}' long.txt this a 10.输出第二列不包含 "th",并打印第二列与第四列 [root@127-0-0-1 scripts]# awk '$2 !~ /th/ {print $2,$4}' long.txt Are like a 11.匹配包含27为数字开头的行,如27,277,2777... awk '/[2][7][7]*/{print $0}' /etc/passwd 匹配包含27为数字开头的行,如27,277,2777... 12.awk自定义输出 awk '{print $1 $3}' part01 //$1与$3相连输出,不分隔 awk '{print $1,$3}' part01 //多了一个逗号,$1与$3使用空格分隔 awk '{print $1 " " $3}' part01 //$1与$3之间手动添加空格分隔 13.awk对字段数量做判断的输出 awk '{print $NF}' part01 //将每行第NF个字段的值打印出来 awk 'NF==4 {print }' part01 //显示只有4个字段的行 awk 'NF>

2 {print $0} 'part01 / / displays rows with more than 2 fields per row

14.awk 's processing of rows

Awk'{print NR,NF,$NF, "\ t", $0} 'part01 / / prints the line number, the number of fields, the last field value, the tab, and each line

Awk 'NR==5 {print}' part01 / / displays line 5

Awk 'NR==5 | | NR==6 {print}' part01 / / shows lines 5 and 6

Awk 'NRemote1 {print} 'part01 / / does not display the first line

15. Matching character processing

/ / Pure character matching! / / Pure character mismatch ~ / / Field values match! ~ / / Field values do not match ~ / A1 | a2 / Field values match A1 or a2

Awk'/ mail/,/mysql/ {print}'/ etc/passwd / / interval matching

Awk'/ [2] [7] [7] * / {print $0}'/ etc/passwd / / matches lines that start with 27 numbers, such as 27, 277, 2777.

Awk'$1~/mail/ {print $1}'/ etc/passwd / / $1 is displayed only if the specified content is matched.

Awk'{if ($1~/mail/) print $1}'/ etc/passwd / / same as above

Awk'$1 matching account account mailbox / {print $1}'/ etc/passwd / / mismatch

16.IF statement, which must be used in {} and the comparison is expanded with ()

Awk'{if ($1~/mail/) print $1}'/ etc/passwd / / abbreviation

Awk'{if ($1~/mail/) {print $1}}'/ etc/passwd / / write all

Awk'{if ($1~/mail/) {print $1} else {print $2}}'/ etc/passwd / / if...else...

17. Conditional expression =! > > =

Awk'$1 part01 = "183.198.46.6" {print $4} 'part01

Awk'{if ($1percent = "mysql") print $3}'/ etc/passwd / / same as above

Awk'$1percent = "mysql" {print $3}'/ etc/passwd / / is not equal to

Awk'$3 > 1000 {print $3}'/ etc/passwd / / greater than

Awk'$3 > = 100 {print $3}'/ etc/passwd / is greater than or equal to

Awk'$38 {print} 'part01 / / logic matches mail with $1, and $3 > 8

Awk'{if ($1~/mail/ & & $3 > 8) print}'/ etc/passwd

Awk'$1~/mail/ | | $3 > 1000 {print}'/ etc/passwd / / logical or

Awk'{if ($1~/mail/ | | $3 > 1000) print}'/ etc/passwd

19. Numerical operation

Awk'$3 > 100' / etc/passwd

Awk'$3 > 100 | | $3

< 5' /etc/passwd awk '$3+$4 >

200'/ etc/passwd

Awk'/ mysql | mail/ {print $3 million 10}'/ etc/passwd / / the third field plus 10 printing

Awk'/ mysql/ {print $3murf 4}'/ etc/passwd / / subtraction

Awk'/ mysql/ {print $3 times 4}'/ etc/passwd / / multiply

Awk'/ MemFree/ {print $2amp 1024}'/ proc/meminfo / / Division

Awk'/ MemFree/ {print int ($2y1024)}'/ proc/meminfo / / rounding

20. Formatted output

Awk'{printf "%-8d%-8s%-10s\ n", $1 part01

Printf representation format output

% format output delimiter,-8 length is 8 digits, s represents string type, print the first three fields of each line, specify the first field output string type (length 8), the second field output string type (length 8), the third field output string type (length 10)

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

Servers

Wechat

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

12
Report