将grep的-v选项与-A选项结合使用 [英] Combine -v option for grep with -A option
问题描述
我想问是否可以结合-v和-A?
我有示例文件:
abc
1
2
3
ACB
def
abc
1
2
3
ABC
xyz
与-AI可以看到我想要剪切的部分:
$ grep abc -A 4 grep_v_test.txt
abc
1
2
3
ACB
-
abc
1
2
3
ABC
它有一些选项可以指定某些内容仅用于查看
def
xyz
?
我发现这个答案 - 在grep中结合-v标志和-A标志但它不适用于我,我试过了
$ sed -e/ abc / {2; 2; d}grep_v_test.txt
sed:-e表达式#1,字符8:未知命令:`;'
也
$ sed/ abc / 2dgrep_v_test.txt
sed:-e表达式#1,字符6:未知命令:`2'
或
$ sed/ abc / + 2dgrep_v_test.txt
sed:-e表达式#1,字符6:未知命令:`+'
$ sed --version
GNU sed版本4.2.1
edit1: c $ c>我希望行abc和1被删除,但其余的将被打印 非常相似它与sed edit2:
根据评论我对这两种解决方案都进行了一些尝试,但是它不起作用,因为我想为 grep -v -A 1 abc $
awk'c&&! - c; / abc / {c = 2}'grep_v_test.txt
只打印包含2的行,这不是我想要的。
$ sed -n'/ abc / {n; n; p}'grep_v_test.txt
2
2
它看来,我无法正确描述它,让我再试一次。
$ b $ <什么 grep -AN abc文件
确实是在abc后面打印N行。我想删除 grep -A
会显示的内容,所以在一个文件中
abc
1
2
3
ACB
def
DEF
abc
1
2
3
ABC
xyz
XYZ
我会只需将abc部分删除到ABC,然后打印剩下的部分:
abc
1
2
3
ACB
def
DEF
abc
1
2
3
ABC
xyz
XYZ
... awk解决方案只打印def和xyz并跳过DEF和XYZ ......
跳过5行上下文从最初的匹配行开始:
$ awk'/ abc / {c = 5} c&& c-- {next} 1'文件
def
xyz
请参阅为匹配模式提取第N行为其他相关脚本。
wrt下面的评论,这里是这个答案和 @ fedorqui的答案之间的区别:
$ cat file
现在是我们不满的冬季
abc
1
2
bar
$ awk'/ abc / {c = 3} c&& c - {next} 1'文件
现在是我们不满$ b的冬季
$ b bar
$ awk'/ abc / {c = 0} c ++> 2'文件
bar
看看@ fedorqui的脚本如何无条件地跳过文件的前两行?
I'd like to ask is it possible to combine somehow -v with -A?
I have example file:
abc
1
2
3
ACB
def
abc
1
2
3
ABC
xyz
with -A I can see the parts I want to "cut":
$ grep abc -A 4 grep_v_test.txt
abc
1
2
3
ACB
--
abc
1
2
3
ABC
it there some option to specify something to see only
def
xyz
?
I found this answer - Combining -v flag and -A flag in grep but it is not working for me, I tried
$ sed -e "/abc/{2;2;d}" grep_v_test.txt
sed: -e expression #1, char 8: unknown command: `;'
also
$ sed "/abc/2d" grep_v_test.txt
sed: -e expression #1, char 6: unknown command: `2'
or
$ sed "/abc/+2d" grep_v_test.txt
sed: -e expression #1, char 6: unknown command: `+'
Sed version is:
$ sed --version
GNU sed version 4.2.1
edit1: Based on comment I experimented a little bit with both solution, but it is not working as I want to
for grep -v -A 1 abc
I would expect line abc and 1 to be removed, but the rest will be printed awk 'c&&!--c; /abc/ {c=2}' grep_v_test.txt
prints just the line containing 2, which is not what I wanted.
Very similar it is with sed
$ sed -n '/abc/{n;n;p}' grep_v_test.txt
2
2
edit2: It seems, I'm not able to describe it properly, let me try again.
What grep -A N abc file
does is to print N lines after abc. I want to remove what grep -A
will show, so in a file
abc
1
2
3
ACB
def
DEF
abc
1
2
3
ABC
xyz
XYZ
I'll just remove the part abc to ABC and I'll print the rest:
abc
1
2
3
ACB
def
DEF
abc
xyz
1
2
3
ABC
XYZ
so 4 lines will remain... The awk solution prints just def and xyz and skips DEF and XYZ...
To skip 5 lines of context starting with the initial matching line is:
$ awk '/abc/{c=5} c&&c--{next} 1' file
def
xyz
See Extract Nth line after matching pattern for other related scripts.
wrt the comments below, here's the difference between this answer and @fedorqui's answer:
$ cat file
now is the Winter
of our discontent
abc
1
2
bar
$ awk '/abc/{c=3} c&&c--{next} 1' file
now is the Winter
of our discontent
bar
$ awk '/abc/ {c=0} c++>2' file
bar
See how the @fedorqui's script unconditionally skips the first 2 lines of the file?
这篇关于将grep的-v选项与-A选项结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!