将文件逐行读入,以空格为默认分隔符将每行切片,切开的部分再进行分析处理
基本用法:
awk [选项参数] ‘/pattern1/{action1} /pattern2/{action2}…’ filename
pattern: 表示awk再数据中查找的内容,就是匹配模式
action: 在找到匹配内容时所执行的一系列命令
选项参数说明:
-F: 指定输入文件分隔符
-v: 赋值一个用户定义变量
内置变量:
FILENAME: 文件名
NR: 已读的记录数(行号)
NF: 浏览记录的域的个数
cat /etc/passwd | awk -F “:” ‘/^root/{print $7}’
[root@localhost jiaoben]# cat /etc/passwd | grep ^root
root:x:0:0:root:/root:/bin/bash
[root@localhost jiaoben]# cat /etc/passwd | grep ^root | cut -d ":" -f 7
/bin/bash
[root@localhost jiaoben]# cat /etc/passwd | awk -F ":" '/^root/{print $7}'
/bin/bash
cat /etc/passwd | awk -F “:” ‘/^root/{print $1",“$6”,"$7}’
[root@localhost jiaoben]# cat /etc/passwd | grep ^root | cut -d ":" -f 1,6,7
root:/root:/bin/bash
[root@localhost jiaoben]# cat /etc/passwd | awk -F ":" '/^root/{print $1,$7}'
root /bin/bash
[root@localhost jiaoben]# cat /etc/passwd | awk -F ":" '/^root/{print $1","$6","$7}'
root,/root,/bin/bash
cat /etc/passwd | awk -F “:” ‘BEGIN{print “user, shell”} {print $1","$7} END{print “root, /bin/aaa”}’
[root@localhost ~]# cat /etc/passwd | awk -F ":" 'BEGIN{print "user, shell"} {print $1","$7} END{print "root, /bin/aaa"}'
user, shell
root,/bin/bash
bin,/sbin/nologin
daemon,/sbin/nologin
adm,/sbin/nologin
lp,/sbin/nologin
sync,/bin/sync
shutdown,/sbin/shutdown
halt,/sbin/halt
mail,/sbin/nologin
operator,/sbin/nologin
games,/sbin/nologin
ftp,/sbin/nologin
nobody,/sbin/nologin
systemd-network,/sbin/nologin
dbus,/sbin/nologin
polkitd,/sbin/nologin
sshd,/sbin/nologin
postfix,/sbin/nologin
chrony,/sbin/nologin
rpc,/sbin/nologin
gluster,/sbin/nologin
libstoragemgmt,/sbin/nologin
qemu,/sbin/nologin
unbound,/sbin/nologin
tss,/sbin/nologin
usbmuxd,/sbin/nologin
saned,/sbin/nologin
colord,/sbin/nologin
abrt,/sbin/nologin
geoclue,/sbin/nologin
saslauth,/sbin/nologin
ntp,/sbin/nologin
setroubleshoot,/sbin/nologin
rpcuser,/sbin/nologin
nfsnobody,/sbin/nologin
rtkit,/sbin/nologin
pulse,/sbin/nologin
radvd,/sbin/nologin
sssd,/sbin/nologin
gdm,/sbin/nologin
gnome-initial-setup,/sbin/nologin
tcpdump,/sbin/nologin
avahi,/sbin/nologin
root, /bin/aaa
BEGIN 在所有数据读取行之前执行;END 在所有数据执行之后执行
awk -v i=2 ‘{print $1+2}’ cut.txt
cut.txt
1 张三 男 28
2 李四 男 31
3 Lily 女 32
4 Lucy 女 29
5 Jack 男 27
[root@localhost jiaoben]# cat cut.txt
1 张三 男 28
2 李四 男 31
3 Lily 女 32
4 Lucy 女 29
5 Jack 男 27
[root@localhost jiaoben]# awk -v i=2 '{print $1+2}' cut.txt
3
4
5
6
7
awk ‘{print “filename:” FILENAME", linenum:" NR “,col:” NF}’ cut.txt
[root@localhost jiaoben]# cat cut.txt
1 张三 男 28
2 李四 男 31
3 Lily 女 32
4 Lucy 女 29
5 Jack 男 27
[root@localhost jiaoben]# awk '{print "filename:" FILENAME", linenum:" NR ",col:" NF}' cut.txt
filename:cut.txt, linenum:1,col:4
filename:cut.txt, linenum:2,col:4
filename:cut.txt, linenum:3,col:4
filename:cut.txt, linenum:4,col:4
filename:cut.txt, linenum:5,col:4
ifconfig | awk ‘/^$/{print NR}’
[root@localhost jiaoben]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.111.15 netmask 255.255.255.0 broadcast 192.168.111.255
inet6 fe80::709d:adbe:cb11:42ef prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:89:a9:62 txqueuelen 1000 (Ethernet)
RX packets 1432 bytes 123824 (120.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 941 bytes 106953 (104.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:56:3e:5e txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost jiaoben]# ifconfig | awk '/^$/{print NR}'
9
18
26
ifconfig ens33 | awk ‘/netmask/ {print $2}’
[root@localhost jiaoben]# ifconfig ens33 | awk '/netmask/ {print $2}'
192.168.111.15
cat cut.txt | awk ‘{print substr($2,2)}’
cat cut.txt | awk ‘{print substr($2,2,2)}’
cat cut.txt | awk ‘{print substr($2,2,1)}’
[root@localhost jiaoben]# cat cut.txt
1 张三 男 28
2 李四 男 31
3 Lily 女 32
4 Lucy 女 29
5 Jack 男 27
[root@localhost jiaoben]# cat cut.txt | awk '{print substr($2,2)}'
三
四
ily
ucy
ack
[root@localhost jiaoben]# cat cut.txt | awk '{print substr($2,2,2)}'
三
四
il
uc
ac
[root@localhost jiaoben]# cat cut.txt | awk '{print substr($2,2,1)}'
三
四
i
u
a
cat cut.txt | awk ‘{print length}’
[root@localhost jiaoben]# cat cut.txt
1 张三 男 28
2 李四 男 31
3 Lily 女 32
4 Lucy 女 29
5 Jack 男 27
[root@localhost jiaoben]# cat cut.txt | awk '{print length}'
9
9
11
11
11
cat cut.txt | awk ‘BEGIN{total=0}{total+=$4}END{print total}’
cat cut.txt | awk ‘BEGIN{total=0}{total+=$4}END{print total/NR}’
[root@localhost jiaoben]# cat cut.txt
1 张三 男 28
2 李四 男 31
3 Lily 女 32
4 Lucy 女 29
5 Jack 男 27
[root@localhost jiaoben]# cat cut.txt | awk 'BEGIN{total=0}{total+=$4}END{print total}'
147
[root@localhost jiaoben]# cat cut.txt | awk 'BEGIN{total=0}{total+=$4}END{print total/NR}'
29.4