awk--命令详解
awk
功能:既是一个命令,又是一种编程语言,可以用来处理数据和生成报告
语法格式:
awk 【option】 ‘pattern{action}’ file
awk 参数 ‘条件{动作}’ 文件
[root@template ~ 15:03:30]]# awk 'NR==2,NR==6' test.txt #显示2到6行内容
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
[root@template ~ 15:03:52]]# awk '{print NR,$0}' test.txt #给每行的内容加上行号 ,$0代表一整行的内容
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
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
18 abrt:x:173:173::/etc/abrt:/sbin/nologin
19 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
20 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
21 rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
22 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
23 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
24 ntp:x:38:38::/etc/ntp:/sbin/nologin
25 nginx:x:111:1000::/home/nginx:/sbin/nologin
26 openvpn:x:998:996:OpenVPN:/etc/openvpn:/sbin/nologin
27 redis:x:2000:2000::/home/redis:/sbin/nologin
28 oldboy:x:2001:2001::/home/oldboy:/bin/bash
[root@template ~ 15:05:21]]# awk 'NR==2,NR==6{print NR,$0}' test.txt #显示2到6行的内容并显示行号
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
参数 -F 指定字符分隔符
[root@template ~ 15:07:05]]# awk -F ":" '{print $1,$3,$NF}' test.txt #打印出文件中的第一列,第三列,最后一列,以冒号为分隔符
root 0 /bin/bash
bin 1 /sbin/nologin
daemon 2 /sbin/nologin
adm 3 /sbin/nologin
lp 4 /sbin/nologin
sync 5 /bin/sync
shutdown 6 /sbin/shutdown
halt 7 /sbin/halt
mail 8 /sbin/nologin
operator 11 /sbin/nologin
games 12 /sbin/nologin
ftp 14 /sbin/nologin
nobody 99 /sbin/nologin
systemd-network 192 /sbin/nologin
dbus 81 /sbin/nologin
polkitd 999 /sbin/nologin
tss 59 /sbin/nologin
abrt 173 /sbin/nologin
sshd 74 /sbin/nologin
postfix 89 /sbin/nologin
rpc 32 /sbin/nologin
rpcuser 29 /sbin/nologin
nfsnobody 65534 /sbin/nologin
ntp 38 /sbin/nologin
nginx 111 /sbin/nologin
openvpn 998 /sbin/nologin
redis 2000 /sbin/nologin
oldboy 2001 /bin/bash
[root@template ~ 15:10:34]]# awk -F ":" 'NR==2,NR==6{print NR,$1,$3,$NF}' test.txt #打印出文件中第二到第六行中第一列,第三列,最后一列的内容并显示出行号
2 bin 1 /sbin/nologin
3 daemon 2 /sbin/nologin
4 adm 3 /sbin/nologin
5 lp 4 /sbin/nologin
6 sync 5 /bin/sync
[root@template ~ 15:16:53]]# awk 'NR==2,NR==6{gsub("/sbin/nologin","/bin/bash",$0);print NR,$0}' test.txt #将文本中的/sbin/nologin替换为/bin/bash
2 bin:x:1:1:bin:/bin:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/bin/bash
4 adm:x:3:4:adm:/var/adm:/bin/bash
5 lp:x:4:7:lp:/var/spool/lpd:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
gsub格式
gsub(“匹配的内容”,“替换成什么”,哪一列)
取ip地址
方法1:
[root@template ~ 15:25:03]]# ifconfig eth0|awk -F "(inet )|( netmask)" 'NR==2{print $2}'
10.0.0.230
[root@template ~ 15:25:15]]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.230 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::d0dc:1fc7:38d0:bfcc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4b:80:be txqueuelen 1000 (Ethernet)
RX packets 4670 bytes 384670 (375.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3297 bytes 391429 (382.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
方法2:
[root@template ~ 15:27:11]]# ifconfig eth0|awk -F "[ ]+" 'NR==2{print $3}'
10.0.0.230
[root@template ~ 15:27:32]]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.230 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::d0dc:1fc7:38d0:bfcc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4b:80:be txqueuelen 1000 (Ethernet)
RX packets 4859 bytes 400198 (390.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3433 bytes 405237 (395.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0