Location>code7788 >text

awk command

Popularity:368 ℃/2024-11-14 17:09:28

awk

[1], awk role

1. Fetch line
2. Fetch columns
3. Fuzzy filtering
4. Judgmental Comparison
string comparison
numerical comparison
5. Support if for while arrays
6. Formatting output

GNU/AWK
programming language

Syntax structure.
awk 'pattern' file # Default is output, no need to add actions.
awk 'mode+action' file
Output of other commands | input to awk commands
df -h|awk 'NR==2'

2】、awk fetch line

Syntax structure:
awk 'NR==3' file
NR awk's built-in variable that stores the line number of each line
The symbol
== Equal to line number
> greater than line number
> = greater than or equal to line number
< less than
<= less than or equal to rows
! = unequal
&& and is similar to sed's 1,3
|| or
Case 1: awk takes out the third line of the file
[root@kylin-xu ~]# awk 'NR==3' passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin

Case 2: awk out file lines greater than 77
[root@kylin-xu ~]# awk 'NR>7' passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
root:x:11:0:operator:/root:/sbin/nologin

Example 3: awk takes out lines less than 3 in a file
[root@kylin-xu ~]# awk 'NR<3' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

Case 4: awk out lines in the text not equal to 1
[root@kylin-xu ~]# awk 'NR!=1' passwd

Case 5: Finding lines greater than 3 and less than 5
[root@kylin-xu ~]# awk 'NR>3&&NR<5' passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin

Case 6: Finding line 3 or 5
[root@kylin-xu ~]# awk 'NR==3||NR==5' passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

3】、awk fuzzy filter

Syntax structure: Regular support
sed -n '//p' file
awk '//' file # Fuzzy search for strings in a file.
awk '//,//' file # Interval range
Case 1: Finding the root line
[root@kylin-xu ~]# awk '/root/' passwd
root:x:0:0:root:/root:/bin/bash
root:x:11:0:operator:/root:/sbin/nologin

Case 2: Finding lines containing bash
[root@kylin-xu ~]# awk '/bash/' passwd
root:x:0:0:root:/root:/bin/bash

Case 3: Finding lines that start with a
[root@kylin-xu ~]# awk '/^a/' passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin

Case 4: Finding lines in a file that contain root or adm
[root@kylin-xu ~]# awk '/root|adm/' passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
root:x:11:0:operator:/root:/sbin/nologin

Case 5: Finding lines that start with b or s
[root@kylin-xu ~]# awk '/^[bs]/' passwd
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Case 6: Filtering lines beginning with adm to lines beginning with halt
[root@kylin-xu ~]# awk '/^adm/,/^halt/' passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/sbin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

[root@kylin-xu ~]# df -h | awk '/run$/,/\/$/'
tmpfs 979M 22M 957M 3% /run
tmpfs 979M 0 979M 0% /sys/fs/cgroup
/dev/mapper/klas-root 39G 3.7G 35G 10% /

[4], awk fetch column

Syntax structure.
awk '{print $1}' file # Fetch the first column in the file
By default, columns are separated by space or tab.
If there is no space or tab, awk will treat the whole line as a column.
In awk all strings in actions are treated as variables, with double quotes they are treated as normal strings.

awk built-in variables
$0 # means a whole line
$1 # Column 1 of the file
$2 # The second column of the file
, # comma for space
NF # column number of the last column of each line
Case 1: Take out the first column
[root@kylin-xu ~]# awk '{print $1}'
a
d
a
a

Case 2: Take out the first and second columns, double quotes are treated as normal strings
[root@kylin-xu ~]# awk '{print $1,$2}'
a b
d e
a s
a x
[root@kylin-xu ~]# awk '{print $1""$2}'
a b
d e
a s
a x
[root@kylin-xu ~]# awk '{print $1","$2}'
a,b
d,e
a,s
a,x

Case 3: Take out the second and first columns, reversed
[root@kylin-xu ~]# awk '{print $2","$1}'
b,a
e,d
s,a
x,a

Case 4: Take out the last column in the file, NF is a specific number that indicates how many columns are in each line
[root@kylin-xu ~]# awk '{print NF}'
3
3
4
5
[root@kylin-xu ~]# awk '{print $NF}'
c
f
g
b

[root@kylin-xu ~]# df -h| awk '{print $NF}'
Mount point
/dev
/dev/shm
/run
/sys/fs/cgroup
/dev/shm /run
/tmp
/backup
/boot
/run/user/0

Specify a separator

awk specifies delimiters
Syntax structure.
awk -F: '{print $1}' # First method of specifying separators
awk -F ":" '{print $1}' # Second way to specify delimiter.
Case 1: Take out the first column in the passwd file
awk -F: '{print $1}' passwd

Case 2: Counting grades
[root@kylin-xu ~]# cat
Zhangsan Linux96 100
Li Si Linux96 22
Lao Wang Linux96 33
Zhao6 Linux96 44
[root@kylin-xu ~]# awk '{print $1,3}' | sort -rn -k2
Zhang San 100
ZhaoLiu 44
Lao Wang 33
Li Si 22

Case 3: Everything is divisible
[root@kylin-xu ~]# echo abca | awk -Fc '{print $1}'
ab

[root@kylin-xu ~]# awk -F ":/" '{print $2}' passwd
root
passwd root
root sbin
var/adm
var/spool/lpd
sbi/adm
var/adm var/spool/lpd sbin
var/spool/lpd sbin
var/spool/mail
root

Case 4: Specify two separators
[root@kylin-xu ~]# awk -F ":|/" '{print NF}' passwd
10
10
10
11
12
10
10
10
12
10

Case 5: Arbitrary characters as separators
[root@kylin-xu ~]# awk -F "[:/]" '{print NF}' passwd

Case 6:
[root@kylin-xu ~]# cat
--:-/test--/::oldboy-oldgirl
[root@kylin-xu ~]# awk -F "[-:/]" '{print $6}'
test
[root@kylin-xu ~]# awk -F "[-:/]+" '{print $2}'
test

awk mode + action

Mode: find specified line by NR awk 'NR==5'
Find line by fuzzy filtering awk '/root/'
awk 'NR==5{print $1}'
Case 1: outputting the second row and second column of a file
[root@kylin-xu ~]# awk -F: 'NR==2{print $2}' passwd
x

Case 2: outputting the last column of the file greater than the sixth line
[root@kylin-xu ~]# awk -F: 'NR>6{print $NF}' passwd
/sbin/shutdown
/sbin/halt
/sbin/nologin
/sbin/nologin

Case 3: Output the first and last columns of the first line
[root@kylin-xu ~]# awk -F: 'NR==1{print $1,$NF}' passwd
root /bin/bash

Case 4: Output the third column of the line containing the adm
[root@kylin-xu ~]# awk -F: '/^adm/{print $3}' passwd
3

Interview Question I have a document, take out the 3rd column of the 5th row in the file

awk 'NR==5{print $3}'

comparison expression

Find rows by character match
Example 1: Finding rows where the first column equals root
[root@kylin-xu ~]# awk -F: '$1=="root"' passwd
root:x:0:0:root:/root:/bin/bash
root:x:11:0:operator:/root:/sbin/nologin

Example 2. Use a regular match string to match the first column of lines ending in n
[root@kylin-xu ~]# awk -F: '$1 ~ "n$"' passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Case 3: Match the last column of lines ending in n
awk -F: '$1!="root"' passwd

Case 4: Invert lines that start with r
awk -F: '$1 ~ "^[^r]"' passwd

Case 5: comparing numbers
[root@kylin-xu ~]# awk -F: '$3==0' passwd
root:x:0:0:root:/root:/bin/bash

Case 6: Third column of rows greater than 5
awk -F: '$3>=5' passwd

[root@kylin-xu ~]# awk '$3>=50'
Zhang San Linux96 100

[5], awk arithmetic operations

Case 1:
[root@kylin-xu ~]# awk 'BEGIN {print 10+1}'
11

Case 2: Outputting the penultimate column
[root@kylin-xu ~]# awk '{print NF-1}'
2
2
3
4
[root@kylin-xu ~]# awk '{print $(NF-1)}'
b
e
f
v

[root@kylin-xu ~]# awk '{print $(NF-1)}'
a
e
f
v