英文:
Output strings via grep excluding matches within them
问题
有没有一种方法,可以输出使用grep
找到的字符串,但排除匹配项?
假设我有一个带有字符串的文件:
>A.123 TextTextTextText....
>B.123 OtherTextTextText....
我想要得到以下结果:
>TextTextTextText...
>OtherTextTextText...
我使用以下命令来搜索这样的字符串:
grep -P '(?<=>)[A-Z0-9\.]*\s' File.txt
它能正确找到匹配项,但我无法弄清如何将所有行输出到单独的文件中,同时排除其中的匹配项。
我知道有一个-o
标志,可以用于仅获取匹配项。是否可能"反转"它?
英文:
Is there a way, to output strings found with grep
, excluding matches?
Let's say I have a file with strings:
>A.123 TextTextTextText....
>B.123 OtherTextTextText....
I would like to get the following:
>TextTextTextText...
>OtherTextTextText...
I use the following command to search for such strings:
grep -P '(?<=>)[A-Z0-9\.]*\s' File.txt
It finds matches correctly, but I can't figure out how to output all lines in a separate file, excluding matches within them.
I know that there is a -o
flag that can be used to get only matches. Is it possible to "invert" it?
答案1
得分: 2
使用 awk
:
$ awk '/^>/ {print ">" $2}' file
>TextTextTextText...
>OtherTextTextText...
英文:
Using awk
:
$ awk '/^>/ {print ">" $2}' file
>TextTextTextText...
>OtherTextTextText...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论