I have a file that looks like that
我有一个看起来像这样的文件
y
z
pattern1
line
1
1
1
patern2
x
k
What I want to do is print the content between the two patterns with the following restrictions
我想要做的是在两种模式之间打印内容,但有以下限制
- Avoid printing the patterns
- 避免打印图案
- Skip the next line after the first pattern
- 在第一个模式后跳过下一行
This means that my output file should look like this
这意味着我的输出文件应如下所示
1
1
1
So far I am able to print between patterns, ignoring them by using
到目前为止,我能够在模式之间打印,通过使用忽略它们
awk '/pattern1/{flag=1;next}/pattern2/{flag=0}flag' file
Any idea on how to do it?
有什么想法怎么做?
3 个解决方案
#1
1
Try this:
尝试这个:
awk '/pattern1/{i=1;next}/patern2/{i=0}{if(i==1){i++;next}}i' File
#2
1
$ awk '/pattern1/,/patern2/{i++} /patern2/{i=0} i>2' file
1
1
1
Between patterns
increment i
, after 2
records start printing (i>2
) and reset i
at the end marker.
在两个模式之间增加i,在2个记录开始打印(i> 2)并在结束标记处重置i之后。
#3
0
you can record the start line number when pattern1 matched:
您可以在pattern1匹配时记录起始行号:
awk '/pattern1/{s=NR+1;p=1;next}/pattern2/{p=0}p&&NR>s' file
The next
could be saved if there is no line matches both pattern1 and pattern2
如果没有与pattern1和pattern2匹配的行,则可以保存下一个
#1
1
Try this:
尝试这个:
awk '/pattern1/{i=1;next}/patern2/{i=0}{if(i==1){i++;next}}i' File
#2
1
$ awk '/pattern1/,/patern2/{i++} /patern2/{i=0} i>2' file
1
1
1
Between patterns
increment i
, after 2
records start printing (i>2
) and reset i
at the end marker.
在两个模式之间增加i,在2个记录开始打印(i> 2)并在结束标记处重置i之后。
#3
0
you can record the start line number when pattern1 matched:
您可以在pattern1匹配时记录起始行号:
awk '/pattern1/{s=NR+1;p=1;next}/pattern2/{p=0}p&&NR>s' file
The next
could be saved if there is no line matches both pattern1 and pattern2
如果没有与pattern1和pattern2匹配的行,则可以保存下一个