Using grep to print just the pattern match

Hi all,

Is it possible for grep to output just the pattern match and not the whole line when it comes across a match? I know you can adjust the number of trailing or leading lines that are printed, but am yet to find anything that outputs just the pattern match.

Cheers,

Tim

In grep, it is -o. I dont know whether this is available in your OS.

grep -o "pattern" FILE

What is the point of this?
grep -l lists the files the pattern is found in,, you already know what the pattern is (although not how many times but a -c would show you that).
Yours intrigued...

I'm guessing, but if it's a REGEX then there is logic. There could be multiple types of match possible, and you might want all of them. In Solaris for example to get a list of disks from format.

That said, standard grep is the wrong tool for this, sed could do it quite well or gnu grep or even awk.

I imagine that "pattern" would contain wildcards, so how would awk or sed do it?

Hope this sed solution helps you,

$ cat t
google gooosdasasdgle derwsdfs
googsdfgle google sreogsds google gsdfsdfd
gooswoerjgle tosgsd towgsjsg googpoiwrwroiegle


$ sed '
s/^.*$/&\n/
:loop
s/^\n//
s/\(.*\)\( *goo.*gle *\)\(.*\)\n/\1\n\2/
t loop' t
google gooosdasasdgle 
googsdfgle google google 
gooswoerjgle googpoiwrwroiegle

The above command prints the words, which matches with the pattern "goo.*gle"