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

How to grep one string after pattern?

I would like to get the string from the file after user defined keyword. Example if keyword is "yellow_y", expected output to be acc.

Tried grep -oP '(?<=yellow_y).*' but not work.

File:

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

yellow      abc \
yellow_x     abc \
yellow_y      acc \
blue     abb \
pink abb \
pink_xx acd \

>Solution :

With your shown samples, please try following grep command. Written and tested in GNU grep.

grep -oP '^yellow_y\s+\K\S+'  Input_file

Online demo for above regex

Explanation: Simple explanation would be, using -oP options of GNU grep which is for printing matched words and enabling PCRE regex respectively. In main program using regex to match condition. Checking if line starts from yellow_y followed by 1 or more spaces then using \K capability of GNU grep to forget this match and matching 1 or more non-spaces characters then which will provide required values in output.

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