学习用例
[root@www ~]# cat test
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog
读入test每行数据并把每行数据打印出来
[root@www ~]# awk '{print $0}' test
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog
打印test第六个字段
[root@www ~]# awk '{print $6}' test
jumps
jumps
jumps
jumps
jumps
打印test最后一个字段
[root@www ~]# awk '{print $NF}' test
dog
dog
dog
dog
dog
提取test第三行数据
指定行号为3
[root@www ~]# awk 'NR==3{print $0}' test
3 the quick brown fox jumps over the lazy cat . dog
指定行的第一个字段精确匹配字符串为3
[root@www ~]# awk '$1=="3"{print $0}' test
3 the quick brown fox jumps over the lazy cat . dog
打印test第三行的第六个字段
[root@www ~]# awk 'NR==3{print $6}' test
jumps
优先级展示
[root@www ~]# awk 'BEGIN{print "hello ayitula"}{print $0}END{print "bye ayitula"}' test
hello ayitula
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog
bye ayitula
不需要数据源,可以直接执行
[root@www ~]# awk 'BEGIN{print "hello world"}'
hello world
没有提供数据流,所以无法执行成功
[root@www ~]# awk '{print "hello world"}'
[root@www ~]# awk 'END{print "hello world"}'
学习用例
[root@www ~]# cat test
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog
打印test文本的行数
[root@www ~]# awk 'END{print NR}' test
5
打印test文本最后一行内容
[root@www ~]# awk 'END{print $0}' test
5 the quick brown fox jumps over the lazy cat . dog
打印test文本列数
[root@www ~]# awk 'END{print NF}' test
12