Friday, February 27, 2009

Print only matched string not line in grep


Suppose in your sample file the word 'bash' is present in different cases (some are BASH,some bash, some Bash etc etc)

Now if you want to see all the occurrences of the string bash, you might not be interested to see the lines containing the different format of the word 'bash'.

So you can use -o option along with -i option with GNU grep

From GREP(1) man pages

-o, --only-matching
Show only the part of a matching line that matches PATTERN.

-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files.

$ cat new.txt
help with bash
bashscripting 0.23
Index and match
last line

So,

$ grep -oi "bash" new.txt
Bash
bash
BASH

1 comment:

S said...

thanks..

I have learbt new thning and solved a problem

© Jadu Saikia www.UNIXCL.com