awk [选项] ‘模式条件{操作}’ 文件1 文件2...
awk -f|-v 脚本文件 文件1 文件.....
FS∶ 列分割符。指定每行文本的字段分隔符,默认为空格或制表位。与"-F"作用相同
NF∶ 当前处理的行的字段个数。
NR∶ 当前处理的行的行号(序数)。
$0∶当前处理的行的整行内容。
$n∶ 当前处理行的第n个字段(第n列)。
FILENAME∶ 被处理的文件名。
RS∶ 行分隔符。awk从文件上读取资料时,将根据RS的定义把资料切割成许多条记录,而awk一次仅读入一条记录,以进行处理。预设值是’\n’
awk可以将自动将多个空格压缩成一个空格
打印字符串需要加双引号
[root@localhost ~]#df |awk '{print $5}'
已用%
10%
0%
0%
1%
0%
1%
18%
1%
1%
0%
[root@localhost ~]#awk '{print "hello"}'
1
hello
2
hello
3
hello
[root@localhost ~]#awk '{print "hello"}' /etc/fstab
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
[root@localhost ~]#awk '{print "hello"}' /etc/fstab |wc -l
18
[root@localhost ~]#cat /etc/fstab |wc -l
18
$n,代表提取第几列
[root@localhost ~]#ifconfig ens33|sed -n '2p'|awk '{print $2}'
192.168.187.150
[root@localhost ~]#ifconfig ens33|sed -n '4p' |awk '{print $2}'
00:0c:29:23:16:e7
[root@localhost ~]#cat /etc/passwd|awk -F":" '{print $1}'
root
bin
daemon
adm
lp
sync
###########后面省略##################
打印时逗号可以表示空格,如果使用“:”或者“+”,需要将特殊符号加上双引号当成字符串打印
[root@localhost ~]#cat /etc/passwd|awk -F":" '{print $1,$2}'
root x
bin x
daemon x
###########后面省略##################
[root@localhost ~]#cat /etc/passwd|awk -F":" '{print $1":"$2}'
root:x
bin:x
daemon:x
adm:x
###########后面省略##################
[root@localhost ~]#cat /etc/passwd|awk -F":" '{print $1"+"$2}'
root+x
bin+x
daemon+x
#使用两条awk命令
[root@localhost ~]#df|awk '{print $5}'|awk -F% '{print $1}'
已用
10
0
0
1
0
18
1
1
0
1
#使用一次awk命令
[root@localhost ~]#df|awk -F"[ %]+" '{print $5}'
已用
10
0
0
1
0
18
1
1
0
1
##也可以使用( |%)代表或者
[root@localhost ~]#df|awk -F"( |%)+" '{print $5}'
已用
10
0
0
1
0
18
1
1
0
1
####文本内容####
[root@localhost opt]#vim test.txt
58.87.87.99 - - [09/Jun/2020:03:42:43 +0800] "POST /wp-cron.php?
doing_wp_cron=1591645363.2316548824310302734375 HTTP/1.1" ""sendfileon
128.14.209.154 - - [09/Jun/2020:03:42:43 +0800] "GET / HTTP/1.1" ""sendfileon
64.90.40.100 - - [09/Jun/2020:03:43:11 +0800] "GET /wp-login.php HTTP/1.1"
""sendfileo
##提取
[root@localhost opt]#cat test.txt |sed '2d'|awk -F"[[ ]" '{print $5"\t"$1}'
09/Jun/2020:03:42:43 58.87.87.99
09/Jun/2020:03:42:43 128.14.209.154
09/Jun/2020:03:43:11 64.90.40.100
""sendfileo
[root@localhost opt]#vim host.txt
1 www.kgc.com
2 mail.kgc.com
3 ftp.kgc.com
4 linux.kgc.com
5 blog.kgc.com
[root@localhost opt]#cat passwd |awk -F":" '/^root/{print}'
cat /etc/passwd |awk -F":" '/root/{print}'
[root@localhost opt]#cat /etc/passwd |awk -F":" '/nologin$/{print}'
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
###########后面省略######
##如果提取/sbin/nologin,则需要\转义/
[root@localhost opt]#cat passwd |awk -F":" '/\/sbin\/nologin$/{print}'
[root@localhost opt]#cat /etc/passwd |awk -F":" '/\/sbin\/nologin$/{print}'
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@localhost opt]#ifconfig ens33|awk '/netmask/{print $2}'
#先定义变量x,表示执行多少次
[root@localhost opt]#awk 'BEGIN {x=0};/\/bin\/bash$/;{x++};END{print x}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
tan:x:1000:1000:tan:/home/tan:/bin/bash
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
45
[root@localhost opt]#awk -v FS=: '{print $1FS$3}' /etc/passwd
root:0
bin:1
daemon:2
adm:3
lp:4
###########后面省略######
[root@localhost opt]#cat /etc/passwd |awk -F: '{print NF}'
7
7
7
###########后面省略######
[root@localhost opt]#cat /etc/passwd |awk -F: '{print $NF}'
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
[root@localhost opt]#cat /etc/passwd |awk -F: '{print $(NF-1)}'
/root
/bin
/sbin
/var/adm
[root@localhost opt]#df|awk '{print $(NF-1)}'
已用%
10%
0%
0%
1%
0%
18%
1%
1%
0%
1%
[root@localhost opt]#awk -F":" '{print $1,NR}' /etc/passwd
root 1
bin 2
daemon 3
adm 4
lp 5
sync 6
shutdown 7
halt 8
#可以用制表符
[root@localhost opt]#awk -F":" '{print $1"\t"NR}' /etc/passwd
root 1
bin 2
daemon 3
adm 4
lp 5
#NR写在前面则在前面显示行号
[root@localhost opt]#awk -F":" '{print NR"\t"$1}' /etc/passwd
1 root
2 bin
3 daemon
4 adm
5 lp
[root@localhost opt]#awk -F: 'NR==2 {print $1}' /etc/passwd
bin
[root@localhost opt]#awk -F: 'NR%2==0 {print $1}' /etc/passwd
bin
adm
sync
halt
operator
ftp
systemd-network
polkitd
libstoragemgmt
[root@localhost opt]#awk -F: 'NR%2==1 {print $1}' /etc/passwd
root
daemon
lp
shutdown
mail
games
nobody
[root@localhost opt]#awk -F: 'NR==1,NR==4 {print $1}' /etc/passwd
root
bin
daemon
adm
#取uid大于1000的行
[root@localhost opt]#awk -F: '$3>1000 {print}' /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
#取uid大于等于1000的行
[root@localhost opt]#awk -F: '$3>=1000 {print}' /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
tan:x:1000:1000:tan:/home/tan:/bin/bash
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
[root@localhost opt]#cat /etc/fstab |awk '/[^#]/{print}'
#/etc/fstab
#Created by anaconda on Sat Sep 4 02:48:23 2021
#Accessible filesystems, by reference, are maintained under '/dev/disk'
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos-root / xfs defaults 0 0
UUID=183ca7c7-1989-4f43-9e81-d2676192f5a4 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost opt]#cat /etc/fstab |awk '/^[^#]/{print}'|awk '{print $3}'|sort |uniq -c
1 swap
3 xfs
[root@localhost opt]#vim text.txt
http://mail.kgc.com/index.html
http://www.kgc.com/test.html
http://study.kgc.com/index.html
http://blog.kgc.com/index.html
http://www.kgc.com/images/logo.jpg
http://blog.kgc.com/20080102.html
http://www.kgc.com/images/kgc.jpg
[root@localhost opt]#cat text.txt |awk -F"[. /]" '{print $3}' >>text.txt
[root@localhost opt]#cat text.txt
http://mail.kgc.com/index.html
http://www.kgc.com/test.html
http://study.kgc.com/index.html
http://blog.kgc.com/index.html
http://www.kgc.com/images/logo.jpg
http://blog.kgc.com/20080102.html
http://www.kgc.com/images/kgc.jpg
mail
www
study
blog
www
blog
www
[root@localhost opt]#grep -Eo "\b[[:alpha:]]+\b" /etc/fstab |sort|uniq -c|sort
1 Accessible
1 anaconda
1 and
1 are
1 blkid
1 boot
1 Created
1 disk
1 etc
1 filesystems
1 findfs
1 for
1 info
1 maintained
1 man
1 more
1 mount
1 on
1 or
1 pages
1 reference
1 root
1 Sat
1 See
1 Sep
1 under
1 UUID
2 by
2 fstab
2 home
3 centos
3 mapper
3 swap
3 xfs
4 defaults
4 dev
[root@localhost opt]#echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw"|grep -E -o "[0-9]+"
05
9
7
3
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/a568911/article/details/121128729
内容来源于网络,如有侵权,请联系作者删除!