参考:朱双印博客
1. 将test文件中的内容打印出来:
vmuser@vmuser-virtual-machine:~/panzidong/awk$ echo ddd > test
vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print}’ test
ddd
2. 输出文件第一列及第二列:
vmuser@vmuser-virtual-machine:~/panzidong/awk$ cat test
111 222 333 444
555 666 777 888
vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print $1,$2}’ test
111 222
555 666
3 一次性输出多个指定的列, 第一列没有第五列就没有输出
vmuser@vmuser-virtual-machine:~/panzidong/awk$ cat test
111 222 333 444
555 666 777 888 999
vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print $1,$2,$5}’ test
111 222
555 666 999
4 可以在输出中添加自已的字段:
vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print $1,$2,”string”}’ test
111 222 string
555 666 string
vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print $1,$2,666}’ test
111 222 666
555 666 666
vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print “first row:”$1,”second row:”$2}’ test
first row:111 second row:222
first row:555 second row:666
vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print “first row:” $1,”second row:” $2}’ test
first row:111 second row:222
first row:555 second row:666
vmuser@vmuser-virtual-machine:~/panzidong/awk$ awk ‘{print “first row:” $1,”middle”,”second row:” $2}’ test
first row:111 middle second row:222
first row:555 middle second row:666
5 内置变量的两侧不能有引号,否则会当作普通字符输出,如下所示:
6 整行输出的方法
7 awk 的两种特殊的模式 BEGIN t和 END模式