Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading