1.打印指定行
Q:如何用awk打印某一文件的第3行之后的所有行?
A:可以利用awk的NR变量。
搜索关键字:
http://search.aol.com/aol/search?q=awk+print+all+but+the+first+three+lines
结论:
$ awk 'NR>10' mem_cpu_usage.txt | wc -l 753 $ wc -l mem_cpu_usage.txt 763 mem_cpu_usage.txt
参考链接:
http://askubuntu.com/questions/475694/awk-command-to-print-all-the-lines-except-the-last-three-lines #有性能比较统计!
2.打印指定列
Q:如何用awk打印某一文件的第3列之后的所有列?
A:利用awk进行编程。
搜索关键字:
http://search.aol.com/aol/search?q=awk+print+all+but+the+first+three+columns
结论:
awk '{for(i=1;i<4;i++) $i="";print}' file awk '{$1=$2=$3="";print}' file awk '{ $1=""; $2=""; $3=""; print $0 }' file
参考链接:
- http://stackoverflow.com/questions/2961635/using-awk-to-print-all-columns-from-the-nth-to-the-last
- http://stackoverflow.com/questions/2626274/print-all-but-the-first-three-columns
- http://superuser.com/questions/211406/awk-print-from-the-third-field-until-end-of-line-each-word-with-one-space
3.去重
awk '{dict[$2]++}; END{for(i in dict) print dict[i]"\t"i}' #count awk '{dict[$2]=dict[$2]+$1}; END{for(i in dict) print dict[i]"\t"i}' #sum awk '{type["all"]+=$3; type[$2]+=$3} END{for(i in type) print i"\t"type[i]}' #sum too awk '{dict[$1]++}; END{for(i in dict) print dict[i]"\t"i}' #count ls -lt web_log/ | awk '$5!=5343{print $5}' | awk '{dict[$1]++}; END{for(i in dict) print dict[i]"\t"i}' find web_log/ -iname "*.txt" | xargs ls -l | awk '$5!=5343{print $5}' | awk '{dict[$1]++}; END{for(i in dict) print dict[i]"\t"i}' find web_log/ -iname "*.txt" | xargs ls -l | awk '$5!=5343{print $5}' | awk '{dict[$1]++; dict["all"]++}; END{for(i in dict) print dict[i]"\t"i}' awk '$5==5343' | wc -l
4.一些应用
awk '$1 !~ /lo/ && NR!=1 && NR!=2' /proc/net/dev | awk 'BEGIN{max=0} {if($2+0 > max+0) max=$2 fi} END{print $1}' | awk -F':' '{print $1}' #获取网卡名称 awk '$1 !~ /lo/' /proc/net/dev | awk 'BEGIN{max=0} {if($2+0 > max+0) max=$2 fi} END{print $1}' | awk -F':' '{print $1}' #获取网卡名称
《 “Awk学习_5” 》 有 2 条评论
如何用awk打印双引号、单引号
`
# 单引号
awk ‘BEGIN{print “