Print only matching words

Hi All,

I have searched the forum and tried to print only matching(pattern) words from the file, but its printing entire line. I tried with grep -w. I am on sunsolaris.

Eg:
cat file
A|A|F1|F2|A|F3|A
A|F10|F11|F14|A|
F20|A|F21|A|F25

I have to search for F[0-9][0-9] (F followed by numbers) and
print distinct
F1,F2,F3,F4....etc

Thanks all for your time and help.

awk -F"|" '{for (i=1;i<=NF;i++){if ($i~/^F/){print $i}}}' infile

Thanks!!

You could also try:

grep -o 'F[0-9]*' file

or

egrep -o 'F[0-9]*' file
   -o, --only-matching
          Show only the part of a matching line that matches PATTERN.

---------- Post updated 06-12-10 at 00:00 ---------- Previous update was 06-11-10 at 23:54 ----------

Oh, I see neither grep nor egrep support the -o option :frowning:
Man Page for grep (OpenSolaris Section 1) - The UNIX and Linux Forums
Man Page for egrep (OpenSolaris Section 1) - The UNIX and Linux Forums

On Solaris /usr/xpg4/bin/grep probably supports -o option.

That would be cool :b:
Let's see if gsjdrr can confirm that...