Pattern Matching

How to print the words with matching pattern. GREP lists the lines matching given pattern. But I want those words that matches the pattern

Please read the man page carefully. -o option is there

echo " this is grep test" | grep -o "test"
test

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

-o option in grep is not there. I am using MKS toolkit that executes Unix shell command in windows environment. Any other way?

but -o option is not available in grep under solaris

-o option is not a standard option to grep

I don't have access to solaris machine right now. If I can recall correctly there are 2 executables for grep in Solaris. Second one should be under /usr/xpg/bin or /usr/xpg4/bin/grep
If not then check to see if egrep is there in your system ( which egrep).

both grep and egrep utilities both searches for the pattern with their own reserved differences in usage

man egrep

the other version of grep is available under /usr/xpg4/bin/grep

and the non-availability of option -o is for both and not just for the /usr/bin/grep

You might have to use awk then.

awk '/pat/{print $x}' filename

where x is the field no. assuming pattern always occurs in the same field.

this dont seem to be valid reply able to apply for all the cases...
or using the above solution directly cannot be generalized

take this sample case,

pattern is 'c i' <c with space and then i>

echo "abc is the pattern" | awk '/c i/ '{printf $1" "$2}'

should be the output

and the pattern spawning over fields of input cannot be precised or cannot be known prior to input....

A possible solution :

awk '{ while (match($0, /pattern/)) { print substr($0,RSTART,RLENGTH); $0 = substr($0, RSTART+1)}' inputfile 

Jean-Pierre.

echo "abc is the pattern" | awk '/c i/ '{printf $1" "$2}'
has a syntactical error, extra ' . Should have given an error message

try this :

echo "abc is the pattern" | awk '/c i/{printf $1"\n"}'

Works fine on my system. Also, I already mentioned that it doesn't encompasses all cases. Depending on the needs, it may or may not be an appropriate solution. I just gave a suggestion.

that was a typo.. i should have been careful

very much true.