awk的相关tips

来源:互联网 发布:不让电脑安装软件 编辑:程序博客网 时间:2024/06/07 05:37
1、awk引用外部文件

awk 'BEGIN {FS=" ";OFS="\t";} {print day,$1,$2 }' day=$date filename


2、利用awk对列进行相加
 cat filename | awk 'BEGIN{FS=OFS="\t";thedate=0;times0=0;times1=0;}{thedate=$1;times0+=$2;times1+=$3;}END{print thedate,times0,times1}'
实例:
cat filename |awk 'BEGIN {FS=" ";OFS="\t";thedate=0;times=0;times1=0} {if($2=="-1" && $3=="-1" && $5=="1") {thedate=$1;times+=$6;times1+=$7}}END {print thedate,times,times1}' 


3、awk查找

文件以tab键为分隔符,查找出第一个字段为空的行,并打印出
cat filename | awk 'BEGIN{FS="\t"}{if($1=="") print }'