一、awk BEGIN
[root@centos8 ~]#awk 'BEGIN{print "hello,awk"}'
[root@centos8 ~]#awk -F: '{print $1":"$3}' /etc/passwd
[root@centos8 ~]#df | awk -F "[[:space:]]+|%" '{print $5}' |sort -nr |head -n1
[root@centos8 ~]#ifconfig eth0 | sed -n '2p' | awk '{print $2}' sed -n '2p'--取行数
二、awk变量
1,常见的内置变量 FS -v变量赋值
[root@centos8 ~]#awk -v FS=':' '{print $1FS$3}' /etc/passwd
2,OFS 输出
[root@centos8 ~]#awk -v FS=':' -v OFS="==" '{print $1,$3}' /etc/passwd
3,RS 输入记录的行
[root@centos8 ~]#echo {a..z} | awk -v RS=' ' -v ORS='==' '{print $0}'
4,NF 字段数量
[root@centos8 ~]#awk -v Fdf |awk -F'[ %]+' '{print $(NF-1)}' | sort -nr| head -n1
5,NR 记录的编号
[root@centos8 ~]#awk -F: '{print NR,$1}' /etc/passwd
[root@centos8 ~]#ifconfig eth0 | awk 'NR==2{print $2}'
6,FNR 个文件分别计数,记录的编号
[root@centos8 ~]#awk '{print FNR}' /etc/passwd /etc/os-release
[root@centos8 ~]#awk '{print FNR,FILENAME}' /etc/passwd /etc/os-release
7,ARGC 命令行参数的个数
[root@centos8 ~]#awk '{print ARGV[1]}' /etc/passwd /etc/os-release
8,自定义变量
[root@centos8 ~]#awk -v test1=test2="hello,gawk" 'BEGIN{print test1,test2}' test2=hello,gawk [root@centos8 ~]#awk -v test1=test2="hello,gawk" 'BEGIN{test1=test2="hello";print test1}' hello
9,printf 格式化输出
[root@centos8 ~]#awk -F: 'BEGIN{printf "------------------------------\n%-20s||%10s||\n-------------------------------\n","username","uid"}{printf "%-20s||%6d||\n------------------------------\n",$1,$3}END{print "结束"}' /etc/passwd
取网卡IP两种方法
[root@centos8 ~]#ifconfig eth0 |awk '/netmask/{print $2}' [root@centos8 ~]#ifconfig eth0 | sed -nr '/netmask/s/.* ([0-9.]+) +netmask.*/\1/p'
10,if-else条件判断
[root@centos8 ~]#awk -F: '{if($3<=100){print "<=,100",$3}else if ($3<=1000) {print "<=1000",$3} else{print ">=1000",$3}}' /etc/passw
11,switcht条件判断
12,while循环
[root@centos8 ~]#awk -v i=1 -v sum=0 'BEGIN{while(i<=100){sum+=i;i++};print sum}' 5050
1,length
[root@centos8 ~]#awk 'BEGIN{print length("abc")}'
2.for循环
[root@centos8 ~]#awk 'BEGIN{sum=0;for(i=1;i<=100;i++){sum+=i};print sum}'
[root@centos8 ~]#sum=0;i=1;while [ $i -le 100 ] ;do let sum+=i;let i++; done;echo $sum
3.next提前结束行的循环
[root@centos8 ~]#awk -F: '{if($3%2!=0) next; print $1,$3}' /etc/passwd
4,数组(默认是关联数组)
[root@centos8 ~]#awk '!line[$0]++' a.txt 去重
5.便利数组
[root@centos8 ~]#awk 'BEGIN{stu[1]="xiaozong";stu[2]="liuzong";stu[3]="sb";for(x in stu){print x":"stu[x]}}'
6.统计服务器链接状态出现的次数
[root@centos8 ~]#ss -nta |awk 'NR!=1{state[$1]++}END{for(i in state){print state[i],i}}'
[root@centos8 ~]#ss -nta |awk 'NR!=1{state[$1]++}END{for(i in state){print i,state[i]}}' 调整后写法
[root@centos8 ~]#ss -nta |awk 'NR!=1{print $1}' | sort |uniq -c 第二种写法
7,链接服务IP出现的次数
[root@centos8 ~]#ss -nt |awk -F"[ :]+" 'NR!=1{ip[ $(NF-2)]++}END{for(i in ip){print ip[i],i}}'| sort -nr
8.字符串处理 sub gsub
[root@centos8 ~]#echo "2008:08:08" | awk 'sub(/:/,"-",$1)' 2008-08:08 [root@centos8 ~]#echo "2008:08:08" | awk 'gsub(/:/,"-",$1)' 2008-08-08
[root@centos8 ~]#awk 'BEGIN{print strftime("%Y-%m-%dT%H:%M",systime()-3600)}'
2023-07-04T10:24
标签:BEGIN,16,etc,awk,print,centos8,root,三剑客 From: https://www.cnblogs.com/liujinxi/p/17522364.html