首页 > 代码库 > awk
awk
#1
awk -F: ‘{print$0 "-->" $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‘ ‘ ‘{print $1}‘
#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
本文出自 “技术屌丝” 博客,谢绝转载!
awk