Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use grep to extract a substring? [duplicate]

Tags:

regex

grep

Possible Duplicate:
extract regexp result from string and write it to a variable

Here is my command :

grep -E '\*[[:space:]]+FIN[[:space:]]+([^)]+?)') myfile

It outputs :

  • FIN (SUCCESS)

And I would like it outputs only :

SUCCESS

How can I tell grep to do it ?

like image 788
Stephan Avatar asked Sep 24 '12 16:09

Stephan


1 Answers

You can pipe the output of your grep command to the awk command.

grep -E '*[[:space:]]+FIN[[:space:]]+([^)]+?)') myfile | awk '{print $2}'

I am not sure how to do that with grep alone, as it is not really tailored to that exact use case. Since you are on a platform where grep is, use pipes to your advantage when you can have one command solve part of the problem, and another command the other part.

like image 188
xshoppyx Avatar answered Sep 29 '22 11:09

xshoppyx