Search code examples
linuxgrep

Output strings via grep excluding matches within them


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?


Solution

  • Using awk:

    $ awk '/^>/ {print ">" $2}' file
    >TextTextTextText...
    >OtherTextTextText...