文件有多少行,取决于文件有多少行
1.
[root@node6 ~]# cat abc
this lineofdatais ignored.
[root@node6 ~]# awk '{print"hello hello"}' abc
hello hello
[root@node6 ~]# echo '' >> abc
[root@node6 ~]# cat abc
this lineofdatais ignored.
[root@node6 ~]#
[root@node6 ~]# awk '{print"hello hello"}' abc
hello hello
hello hello
2.begin 开始
[root@node6 ~]# awk '{print"abc"}'
abc
abc
^C
[root@node6 ~]# awk 'BEGIN{print"abc"}'
abc
[root@node6 ~]#
3.
[root@node6 ~]# cat abc
this lineofdatais ignored.
[root@node6 ~]# awk '/^$/{print"This is a blank line"}' abc
This is a blank line
[root@node6 ~]# echo "">> abc
[root@node6 ~]# cat abc
this lineofdatais ignored.
[root@node6 ~]# awk '/^$/{print"This is a blank line"}' abc
This is a blank line
This is a blank line
[root@node6 ~]#
2. 例
[root@node6 ~]# cat abc
123
a b c
1
a A B C
2
[root@node6 ~]# awk '/[0-9]+/{print"That is an integer"}' abc //查看数字,0到正无穷,0,1,2...任意一次
That is an integer
That is an integer
That is an integer
[root@node6 ~]# awk '/[A-Za-z]+/{print"That is a string"}' abc //查看字母
That is a string
That is a string
[root@node6 ~]# awk '/^$/{print"That is a blank line"}' abc //空行
That is a blank line
That is a blank line
[root@node6 ~]#
3. 例
开始 正文 结束
在begin定义的变量,后面可以使用
[root@node6 ~]# echo "a b c d"|awk 'BEGIN{one=1;two=2}{print $(one + two)}'
c
[root@node6 ~]#
[root@node6 ~]# echo "a b c d"|awk 'BEGIN{one=1;two=2}{print $(one + two)}END{print"1 + 2 = "(1+2)}'
c
1+2=3
[root@node6 ~]#
4. 字段和引用的分离
awk 允许使用字段操作符$来指定字段。在该操作符后面跟着一个数字或变量,用于标识字段的位置。”$1”表示第一个字段,$2表示第二个字段等等。$O”表示整个输入记录。下面的例子显示了第一个字段是姓,第二个字段是名字,后面是电话号码。
[root@node6 ~]# cat abc
john robinson 666-555-1111
[root@node6 ~]# awk '{print $2,$1,$3}' abc
robinson john 666-555-1111
[root@node6 ~]#