本文算是学习的总结,整体是按照菜鸟教程的流程一步步练习的,多一些自己的理解
awk [选项参数] 'script' var=value file(s)
awk [选项参数] -f scriptfile var=value file(s)
1 this is a test
learn awk
3 what do u think?
I have iphone,macbook,ipod
// 输出test.log每一行的第一列,第四列,默认输出分隔符(" ")
awk '{print $1,$4}' test.log
1 a
learn
3 u
I
// 格式化输出,占位符参考注意说明
awk '{printf "%-8s %-10s\n",$1,$4}' test.log
1 a
learn
3 u
I
// 指定","分隔符来处理每一行
awk -F, '{print $1,$2}' test.log
1 this is a test
learn awk
3 what do u think?
I have iphone macbook
// 声明一个变量a,可以看到数字做运算,非数字当作0处理
awk -v a=1 '{print $1,$1+a}' test.log
1 2
learn 1
3 4
I 1
// 筛选第一列大于2的行,可以看到没有{}部分对每行处理即整行输出
awk '$1>2' test.log
learn awk
3 what do u think?
I have iphone,macbook,ipod
// 筛选第一行等于3的,输出第一列第三列
awk '$1>=1 && $2=="what" {print $1,$2,$3}' test.log
3 what do
// 注意说明提到过,BEGIN{}即读取记录之前的处理,在这就是格式化输出了表头和一行横线
// {}是对每行数据的处理,实际值都是内建变量
awk 'BEGIN{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","FILENAME","ARGC","FNR","FS","NF","NR","OFS","ORS","RS";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}' test.log
FILENAME ARGC FNR FS NF NR OFS ORS RS
---------------------------------------------
test.log 2 1 5 1
test.log 2 2 2 2
test.log 2 3 5 3
test.log 2 4 3 4
// 设置内建变量
awk '{print $1,$2,$5}' OFS=" $ " test.log
1 $ this $ test
learn $ awk $
3 $ what $ think?
I $ have $
// ~ 表示模式开始。针对某列的模式需要加~ ‘//’ 中是模式。
// 筛选第二列包含‘h’的行,输出第二列第四列
awk '$2 ~ /h/ {print $2,$4}' test.log
this a
what u
have
// 设置内建变量IGNORECASE忽略大小写,筛选包含i的行
awk 'BEGIN{IGNORECASE=1} /i/' test.log
1 this is a test
3 what do u think?
I have iphone,macbook,ipod
// !标示取反,即筛选不含i的行
awk '!/i/' test.log
learn awk
这里要注意注意说明中的BEGIN{},{},END{}
Marry 2143 78 84 77
Jack 2321 66 78 45
Tom 2122 48 77 71
Mike 2537 87 97 95
Bob 2415 40 57 62
#运行前
BEGIN {
math = 0
english = 0
computer = 0
printf "NAME NO. MATH ENGLISH COMPUTER TOTAL\n"
printf "---------------------------------------------\n"
}
#运行中
{
math+=$3
english+=$4
computer+=$5
printf "%-6s %-6s %4d %8d %8d %8d\n", $1, $2, $3,$4,$5, $3+$4+$5
}
#运行后
END {
printf "---------------------------------------------\n"
printf " TOTAL:%10d %8d %8d \n", math, english, computer
printf "AVERAGE:%10.2f %8.2f %8.2f\n", math/NR, english/NR, computer/NR
}
// -f执行脚本文件,注意格式化输出里的数字为了对其表头,所以数字即表头字符数和空格数
awk -f cal.awk score.txt
NAME NO. MATH ENGLISH COMPUTER TOTAL
---------------------------------------------
Marry 2143 78 84 77 239
Jack 2321 66 78 45 189
Tom 2122 48 77 71 196
Mike 2537 87 97 95 279
Bob 2415 40 57 62 159
---------------------------------------------
TOTAL: 319 393 350
AVERAGE: 63.80 78.60 70.00
// 还可以配合其他指令使用:如计算当前目录所有txt文件大小之和
ls -l *.txt | awk '{sum+=$6} END {print sum}'
666581
// 从文件中找出长度大于80的行
awk 'length>80' log.txt