Output strings via grep excluding matches within them

huangapple go评论58阅读模式
英文:

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:

&gt;A.123 TextTextTextText....
&gt;B.123 OtherTextTextText....

I would like to get the following:

&gt;TextTextTextText...
&gt;OtherTextTextText...

I use the following command to search for such strings:

grep -P &#39;(?&lt;=&gt;)[A-Z0-9\.]*\s&#39; 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 '/^&gt;/ {print ">" $2}' file
>TextTextTextText...
>OtherTextTextText...
英文:

Using awk:

$ awk &#39;/^&gt;/ {print &quot;&gt;&quot; $2}&#39; file
&gt;TextTextTextText...
&gt;OtherTextTextText...

huangapple
  • 本文由 发表于 2023年7月20日 14:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定