# awk [option] 'script' 文件名称 文件名称
# awk [option] 'PATTERN{action}' 文件名称 文件名称
[root@localhost ~]# sed -n '1p' /etc/passwd | awk -F: '{print $1}'
[root@localhost ~]# sed -n '1p' /etc/passwd | awk -F: '{print $0}'
[root@localhost ~]# sed -n '1p' /etc/passwd | awk -F: '{print $1, $3}'
[root@localhost ~]# sed -n '1,3p' /etc/passwd | awk -F: '{print "用户名:",$1}'
[root@localhost ~]# rpm -qa | grep php | awk -F-5 '{print "rpm -e --nodeps", $1}' | bash
[root@localhost ~]# awk '{printf "%s", $2}' /tmp/file01
[root@localhost ~]# awk '{printf "%s%s", $2, $3}' /tmp/file01
1、FS
[root@localhost ~]# sed -n '1p' /etc/passwd | awk -v FS=":" '{print $1}'
2、OFS
[root@localhost ~]# sed -n '1p' /etc/passwd | awk -v OFS="--->" -F: '{print $1, $7}'
root--->/bin/bash
3、NR
[root@localhost ~]# awk '{print NR}' /etc/hosts /etc/redhat-release
[root@localhost ~]# awk 'NR==3{print $0}' /etc/passwd
4、FNR
[root@localhost ~]# awk '{print FNR}' /etc/hosts /etc/redhat-release
5、NF
[root@localhost ~]# sed -n '1p' /etc/passwd | awk -F: '{print NF}'
[root@localhost ~]# awk '{print $NF}' /tmp/file01
6、FILENMAME
[root@localhost ~]# awk '{print FILENAME}' /tmp/file01
/tmp/file01
/tmp/file01
/tmp/file01
[root@localhost ~]# awk '{print FILENAME}' /tmp/file01 | uniq
/tmp/file01
-v 变量名称=值
[root@localhost ~]# awk -v name="martin" '{print "hello", name}' /etc/fstab
[root@localhost ~]# awk -v number=100 '{print number}' /etc/fstab
1、表达式
比较运算符
逻辑运算符
[root@localhost ~]# awk -F: '$3>=1 && $3 <= 999{print $1}' /etc/passwd
[root@localhost ~]# awk -F: '$7=="/sbin/nologin"{print $1}' /etc/passwd
[root@localhost ~]# awk -F: '$7 ~ "nologin$"{print $1}' /etc/passwd
[root@localhost ~]# df -hT | grep "^/dev" | awk '+$6>20{print "磁盘名称:", $1, "剩余空间", $5, "挂载目录:", $NF}'
2、正则表达式
[root@localhost ~]# netstat -antp | awk '/^tcp/{print $0}'
[root@localhost ~]# awk -F: '/^[rhg]/{print $1}' /etc/passwd
[root@localhost ~]# ls -l /etc/ | awk '/^-/{print $NF}'
3、/正则/,/正则/
[root@localhost ~]# awk '/Sep 26 09:02:30/,/Sep 26 09:18:04/{print $0}' /var/log/messages
4、BEGIN{}
[root@localhost ~]# sed -n '1,3p' /etc/passwd | awk -F: 'BEGIN{print "----数据开始----"}{print $1,$7}'
[root@localhost ~]# awk 'BEGIN{name="martin"; print name}'·
5、END{}
[root@localhost ~]# sed -n '1,3p' /etc/passwd | awk -F: 'BEGIN{print "----数据开始-----"}{print $1, $7}END{print "----数据结束----"}'
示例:统计bash用户数量和nologin数量
[root@localhost ~]# awk -F: -v bash_number==0 -v nologin_number==0 '{if ($7=="/bin/bash"){bash_number++}else if ($7=="/sbin/nologin"){nologin_number++}}END{print "bash用户数量:",bash_number,"nologin用户数量:",nologin_number}' /etc/passwd
bash用户数量: 1 nologin用户数量: 17
for(变量定义; 循环条件; 改变变量的值) {操作}
for(i=1; i<=10; i++)
[root@localhost ~]# sed -n '1,3p' /etc/passwd | awk -F: '{for(i=1; i<=7; i++) {if(length($i)>=5) {print $i}}}'
/root
/bin/bash
/sbin/nologin
daemon
daemon
/sbin
/sbin/nologin
[root@localhost ~]# awk '{for(i=1; i<=NF; i++) {if(length($i)>=4) {print $i}}}' /etc/passwd
[root@localhost ~]# awk 'BEGIN{test[1]="martin"; test[2]="robin"; test[3]="lz"}'
[root@localhost ~]# awk 'BEGIN{test[1]="martin"; test[2]="robin"; test[3]="lz"; print test[2]}'
robin
[root@localhost ~]# awk 'BEGIN{test[1]="martin"; test[2]="robin"; test[3]="lz"; for(i=1; i<=3; i++) {print test[i]}}'
[root@localhost ~]# awk 'BEGIN{test["martin"]="北京"; test["robin"]="河北"; test["lz"]="天津"}'
[root@localhost ~]# awk 'BEGIN{test["martin"]="北京"; test["robin"]="河北"; test["lz"]="天津"; print test["robin"]}'
河北
示例1:
打印下标
[root@localhost ~]# awk 'BEGIN{test["martin"]="北京"; test["robin"]="河北"; test["lz"]="天津"; for(i in test) {print i}}'
robin
lz
martin
打印下标对应内容
[root@localhost ~]# awk 'BEGIN{test["martin"]="北京"; test["robin"]="河北"; test["lz"]="天津"; for(i in test) {print test[i]}}'
河北
天津
北京
合并:循环打印
[root@localhost ~]# awk 'BEGIN{test["martin"]="北京"; test["robin"]="河北"; test["lz"]="天津"; for(i in test) {print "姓名:", i, "住.:", test[i]}}'
姓名: robin 住址: 河北
姓名: lz 住址: 天津
姓名: martin 住址: 北京
[root@localhost ~]# awk -F: '{data[$7]++}END{for(i in data) {print i, data[i]}}' /etc/passwd
[root@localhost ~]# awk '{uv[$1]++}END{for(i in uv) {print i, uv[i]}}' access_log
[root@localhost ~]# awk '{pv[$7]++}END{for(i in pv) {print i, pv[i]}}' access_log
1、split(字符串、数组、行分隔符)
[root@localhost ~]# awk 'BEGIN{split("a-b-c", data, "-"); print data[1]}'
[root@localhost ~]# awk 'BEGIN{split("a-b-c", data, "-"); print data[2]}'
[root@localhost ~]# awk 'BEGIN{split("a-b-c", data, "-"); print data[3]}'
2、length(string)
[root@localhost ~]# awk 'BEGIN{print length("shell")}'
3、substr(string,start,[length])
[root@localhost ~]# awk 'BEGIN{data=substr("hello",2, 3); print data}'
ell
4、system(commend)
[root@localhost ~]# awk 'BEGIN{system("ifconfig ens33")}'
5、systime()
[root@localhost ~]# awk 'BEGIN{print systime()}'
1645035840
[root@localhost ~]# awk 'BEGIN{now=systime(); print strftime("%F_%T", now)}'
2022-02-17_02:24:36
6、tolower(string)
[root@localhost ~]# awk 'BEGIN{print tolower("aBcD")}'
abcd
7、toupper(string)
[root@localhost ~]# awk 'BEGIN{print toupper("aBcD")}'
ABCD
035840
* 指定返回的格式
```bash
[root@localhost ~]# awk 'BEGIN{now=systime(); print strftime("%F_%T", now)}'
2022-02-17_02:24:36
6、tolower(string)
[root@localhost ~]# awk 'BEGIN{print tolower("aBcD")}'
abcd
7、toupper(string)
[root@localhost ~]# awk 'BEGIN{print toupper("aBcD")}'
ABCD