#1 应用
awk -F: '{print0"−−>"1 "-->" $2 }' /etc/passwd
#2 多个匹配
awk'/yeqing|mysql|nginx/' /etc/passwd
#3 正则匹配
awk '/^yeqing|mysql$/' /etc/passwd
#4 将小写转换成大写
cat yeqing.txt |tr 'a-z' 'A-Z' > p.txt
或者
awk '/yeqing/'/etc/passwd |tr "a-z" "A-Z" >tmp.txt
#5 取出第二行
ifconfig eth0 |awk -F':' 'NR==2 {print $2}'
ifconfig eth0 |awk -F: 'NR==2{print 2}' |awk-F' ' '{print1}'
#6 取出第二行用“空格”或者“:”去分割
ifconfig eth0 |awk -F '[ :]+' 'NR==2 {print $4}'
#7 统计
awk '{count++}END{print "ct:",count}' /etc/passwd
awk '{i++}END{print i}' /etc/passwd
cat /etc/passwd |wc -l
#8 统计某个文件夹下的文件占用的字节数:
ls -l |awk -F ' ' 'BEGIN {size=0;} {size=size+$5;} END{print "size:",size}'
以M为单位显示
ls -l |awk'BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ",size/1024/1024,"M"}'
#9 分析access.log获得访问前10位的ip地址
awk '{print $1}'access.log |sort|uniq -c|sort -nr|head -10
或者
awk '{print $1}'/usr/local/sina_mobile/nginx/logs/access.log |sort|uniq -c|sort -nr|head -10
tail/usr/local/sina_mobile/nginx/logs/access.log |awk -F ' ' '{print $1}'|sort|uniq -c |sort –nr
#10 分开|过滤|显示
tail /etc/passwd|awk -F ':' '{if(substr(1,0,2)=="ye"){ print0}}'
#11 4舍5入
cat ye.txt |awk-F. '{if(substr(2,1,1)>=5)1+=1 ; print $1}'
或者
cat ye.txt |awk'{print int($1+0.5)}'
#12 分析日志
tail/usr/local/sina_mobile/nginx/logs/access.log |awk -F '- -' '/[GET]/{print $1}'|sort -nru
awk -F "--" '{if(0 /index.html/)print1}' test
#13 生产案例:
[root@home-web1scripts]# cat name
张三 10亿
李四 200万
王五 100千
[root@home-web1scripts]# cat info
上海 张三
北京 李四
南京 王五
[root@home-web1scripts]# awk 'FNR==NR{arr[1]=0;next}{print arr[2],1}' name info
张三 10亿 上海
李四 200万 北京
王五 100千 南京
本文转自cloves 51CTO博客,原文链接:http://blog.51cto.com/yeqing/1598296