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_* .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With