01.awk作用
**更多详细内容参考:**https://www.cnblogs.com/xiaonq/p/11969744.html
1.1 awk作用
awk是一种编程语言,用于在linux下对文本和数据进行处理
awk的处理文件和数据处理方式是逐行扫描,寻找到匹配的行,并在这些行上进行你想要的操作
如果没有指定处理动作,则把匹配的行显示到屏幕上
1.2 awk常用指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // {} $0 $1 NF NR /[0 -9 ][0 -9 ]+/ /[0 -9 ][0 -9 ]*/ -F'[:#/]' FNR \t \n FS RS ~ !~ == != && || +
02.awk使用
2.1 awk常用指令
1 2 3 4 5 6 7 cat /etc/passwd |awk -F ':' '{print $1}' cat /etc/passwd | awk -F ':' '{print $1"\t"$7}' awk -F ':' '/root/{print $1}' /etc/passwd w|awk 'NR==1{print $6}' awk -F: 'NR==5 || NR==6{print}' /etc/passwd awk '!/mysql/' /etc/passwd awk '/mysql|mail/{print}' /etc/passwd
2.2 awk基本语法
1 2 3 4 tail -n3 /etc/services |awk -F'[ /]+' '{print $2}' awk -v a=123 'BEGIN{print a}' tail -n3 /etc/services |awk 'BEGIN{print "服务\t\t端口\t\t\t描述"}{print $0}END{print "===结束==="}'
2.3 正则匹配
1 2 3 4 5 tail /etc/services |awk '/^blp5/{print $0}' tail /etc/services |awk '/^[a-z0-9]{8} /{print $0}' tail /etc/services |awk '/blp5/ && /tcp/{print $0}' awk '! /^#|^$/' /etc/nginx/nginx.conf tail /etc/services |awk '/^blp5/,/^com/'
2.4 NF
1 2 3 4 5 echo "a b c d e f" |awk '{print NF}' echo "a b c d e f" |awk '{print $1}' echo "a b c d e f" |awk '{print $NF}' echo "a b c d e f" |awk '{print $(NF-1)}' echo "a b c d e f" |awk '{$NF="";$(NF-1)="";print $0}'
2.5 NR
1 2 3 tail -n5 /etc/services |awk '{print NR,$0}' tail -n5 /etc/services |awk 'NR==3{print $2}' tail -n5 /etc/services |awk 'NR<=3{print NR,$0}'
2.6 操作符
1 2 3 4 5 '''数字 0,空字符串和未定义的值 ''' seq 3 |awk '{print $0*2}' seq 3 |awk '{print $0/2}' seq 5 |awk '$0%2==0{print $0}' seq 5 |shuf |awk '{print $0|"sort"}'
__END__