Regular Introduction_awk

Mission 18 October

9.6/9.7 awk

 

awk tools

It has more functions than grep and sed tools and can be operated in segments.

Basic use

  • Segment operation
# - F specifies the delimiter, defaulting to space

{}Internal
# $1 means: the first field after segmentation
# $0 represents a line
# First paragraph of printed content
[root@centos7 tmp]# head test.txt | awk -F ':' '{ print $1}' 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator

# Print entire line
[root@localhost ~]# head test.txt | awk -F ":" '{print $0}' 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
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:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

  • Matching for segments
# + Matching Content (no need to add\ escape)
[root@centos7 tmp]# head test.txt | awk -F ':' '$1 ~ /root/'  
root:x:0:0:root:/root:/bin/bash
  • Matching multiple conditions
[root@centos7 tmp]# awk -F ':' '/root/ {print $1,$3} /mail/ {print$1,$3,$4}' test.txt 
root 0
mail 8 12
operator 11

# Specify a delimiter and print out in the same format
[root@centos7 tmp]# awk -F ':' '/root|mail/ {print $1,$3}' test.txt 
root 0
mail 8
operator 11

# Specify the delimiter, print out in different formats, and specify the output delimiter
[root@localhost ~]# awk -F ':' '/root/ {print $1,"#",$3} /mail/ {print$1,"#",$3,"#",$4}' /etc/passwd
root # 0
mail # 8 # 12
operator # 11
  • Conditional judgement

Double quotation marks are needed for string conditions, which are sorted in string order; double quotation marks are not needed for numeric order. Available conditional judgement symbols include: >= <====!=.

# Single Field Conditional Judgment
[root@centos7 tmp]# awk -F ':' '$3>=1000' test.txt 
castiel:x:1000:1000::/home/castiel:/bin/bash

[root@centos7 tmp]# awk -F ':' '$3>=1000 {print $1}' test.txt 
castiel

# === Equality (field comparison)
[root@centos7 tmp]# awk -F ':' '$3==$4' test.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
castiel:x:1000:1000::/home/castiel:/bin/bash

# = = not equal to
[root@localhost ~]# awk -F ':' '$7!="/sbin/nologin" {print $1}' test.txt
root
sync
shutdown
halt
castiel

# Use & or | to achieve multi-conditional judgment
[root@localhost ~]# awk -F ':' '$7!="/sbin/nologin" && $3==$4' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mysql:x:1000:1000::/home/mysql:/bin/bash
  • Use conditional statements
[root@centos7 tmp]# awk -F ':' '{OFS="#"}{if ($3>=1000) {print $1,$3,$4}}' test.txt 
castiel#1000#1000

Built-in variables

# OFS represents the output separator
[root@centos7 tmp]# head test.txt |awk -F ':' '{OFS="#"} {print $1,$3,$4}'
root#0#0
bin#1#1
daemon#2#2
adm#3#4
lp#4#7
sync#5#0
shutdown#6#0
halt#7#0
mail#8#12
operator#11#0

# NR: Number of rows, using $NR corresponds to $1, $2...
[root@centos7 tmp]# awk -F ':' '{print NR,$0}' test.txt | head 
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin

# NF denotes the number of segments per line, where the value of NF is 7, so $NF is equivalent to $7.
[root@localhost ~]# awk -F ':' '{print NR,$NF}' test.txt | head
1 /bin/bash
2 /sbin/nologin
3 /sbin/nologin
4 /sbin/nologin
5 /sbin/nologin
6 /bin/sync
7 /sbin/shutdown
8 /sbin/halt
9 /sbin/nologin
10 /sbin/nologin
  • Assignment: Use =, unlike ==, its output will have no delimiter, which needs to be specified using OFS
[root@centos7 tmp]# head -n 3 test.txt | awk -F ':' '{OFS=":"} $1="root"'
root:x:0:0:root:/root:/bin/bash
root:x:1:1:bin:/bin:/sbin/nologin
root:x:2:2:daemon:/sbin:/sbin/nologin

Extended use: END block

# Calculate the sum of the numbers in the third paragraph
[root@localhost ~]# awk -F ':' '{(tot=tot+$3)}; END {print tot}' test.txt
4661

Tags: Operation & Maintenance network MySQL DBus ssh

Posted on Thu, 31 Jan 2019 05:03:14 -0500 by Pryach