Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In grep on Ubuntu, how can I display only the string that matched the regular expression?

Tags:

grep

ubuntu

I am basically grepping with a regular expression on. In the output, I would like to see only the strings that match my reg exp.

In a bunch of XML files (mostly they are single-line files with huge amounts of data in a line), I would like to get all the words that start with MAIL_.

Also, I would like the grep command on the shell to give only the words that matched and not the entire line (which is the entire file in this case).

How do I do this?

I have tried

grep -Gril MAIL_* .
grep -Grio MAIL_* .
grep -Gro MAIL_* .
like image 609
AMM Avatar asked Aug 06 '10 12:08

AMM


1 Answers

First of all, with GNU grep that is installed with Ubuntu, -G flag (use basic regexp) is the default, so you can omit it, but, even better, use extended regexp with -E.

-r flag means recursive search within files of a directory, this is what you need.

And, you are right to use -o flag to print matching part of a line. Also, to omit file names you will need a -h flag.

The only mistake you made is the regular expression itself. You missed character specification before *. Your command should look like this:

grep -Ehro 'MAIL_[^[:space:]]*' .

Sample output (not recursive):

$ echo "Some garbage MAIL_OPTION comes MAIL_VALUE here" | grep -Eho 'MAIL_[^[:space:]]*'
MAIL_OPTION
MAIL_VALUE
like image 147
thor Avatar answered Oct 13 '22 01:10

thor