Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-11-2010
Registered User
 

Join Date: Oct 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old 06-11-2010
bartus11's Avatar
Registered User
 

Join Date: Apr 2009
Posts: 2,654
Thanks: 3
Thanked 789 Times in 777 Posts

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

Sponsored Links
    #3  
Old 06-11-2010
Registered User
 

Join Date: Oct 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

Thanks!!
    #4  
Old 06-11-2010
pseudocoder's Avatar
Registered User
 

Join Date: Sep 2007
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts
You could also try:

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

or

Code:
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
Man Page for grep (OpenSolaris Section 1) - The UNIX and Linux Forums
Man Page for egrep (OpenSolaris Section 1) - The UNIX and Linux Forums
Sponsored Links
    #5  
Old 06-11-2010
bartus11's Avatar
Registered User
 

Join Date: Apr 2009
Posts: 2,654
Thanks: 3
Thanked 789 Times in 777 Posts
On Solaris /usr/xpg4/bin/grep probably supports -o option.
Sponsored Links
    #6  
Old 06-11-2010
pseudocoder's Avatar
Registered User
 

Join Date: Sep 2007
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts
That would be cool
Let's see if gsjdrr can confirm that...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed/awk: Delete matching words leaving only the first instance GrinningArmor Shell Programming and Scripting 2 01-20-2010 03:31 AM
Matching words in Perl monika Shell Programming and Scripting 2 02-14-2009 04:20 AM
How to from grep command from a file which contains matching words? johnl Shell Programming and Scripting 4 07-29-2008 01:44 AM
matching words using regular expressions bishweshwar UNIX for Advanced & Expert Users 1 12-27-2007 09:28 AM
getting file words as pattern matching arunkumar_mca Programming 5 05-31-2005 03:28 AM



All times are GMT -4. The time now is 03:54 AM.