- 论坛徽章:
- 2
|
本帖最后由 irockey 于 2017-08-16 21:54 编辑
关于awk的范围模式功能问题
man awk中这样写到
The pattern1, pattern2 form of an expression is called a range pattern. It matches all input records starting with a record that matches pattern1, and continuing until a record that matches pattern2, inclusive. It does not combine with any other sort of pattern expression.
表达式的pattern1,pattern2形式称为范围模式。 它匹配所有输入记录,从与pattern1匹配的记录开始,并持续到与pattern2匹配的记录(包括)。 它不与任何其他类型的模式表达式组合。
[root@localhost ~]# awk '/aaa/,/bbb/{print}' file
aaa
grge
ddd
bbb
aaa gege
ccc
bbb
aaa gregeg
eee
bbb
这里的/bbb/并没有起作用,执行了从/aaa/一直匹配到最后一行
[root@localhost ~]# awk '/aaa/,/ccc/{print}' file
aaa
grge
ddd
bbb
aaa gege
ccc
aaa gregeg
eee
bbb
这里/ccc/并没有起作用,跟/bbb/一样,","起作用了,但/bbb/没有匹配到结果
按照awk的man文档说明,awk '/aaa/,/bbb/' file结果应该是
aaa
grge
ddd
bbb
可是这里的结果却是到最后一行,是不是/bbb/这个pattern并没有匹配到
[root@localhost ~]# seq 9 | awk /3/,/7/
3
4
5
6
7
当每行内容不同,awk的范围模式能正确执行
[root@localhost ~]# seq 10 | awk /1/,/7/
1
2
3
4
5
6
7
10
当有重复行时,范围模式会将重复结果显示
想问下,awk的pattern,pattern模式正确的使用方式应该是怎样的
什么时候用awk的范围模式?
|
|