Print only matching words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print only matching words
# 1  
Old 06-11-2010
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.
# 2  
Old 06-11-2010
Code:
awk -F"|" '{for (i=1;i<=NF;i++){if ($i~/^F/){print $i}}}' infile

# 3  
Old 06-11-2010
MySQL

Thanks!!
# 4  
Old 06-11-2010
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 Smilie
Man Page for grep (OpenSolaris Section 1) - The UNIX and Linux Forums
Man Page for egrep (OpenSolaris Section 1) - The UNIX and Linux Forums
# 5  
Old 06-11-2010
On Solaris /usr/xpg4/bin/grep probably supports -o option.
# 6  
Old 06-11-2010
That would be cool Smilie
Let's see if gsjdrr can confirm that...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

2. Shell Programming and Scripting

Find all matching words in text according to pattern

Hello dear Unix shell professionals, I am desperately trying to get a seemingly simple logic to work. I need to extract words from a text line and save them in an array. The text can look anything like that: aaaaaaa${important}xxxxxxxx${important2}ooooooo${importantstring3}...I am handicapped... (5 Replies)
Discussion started by: Grünspanix
5 Replies

3. Shell Programming and Scripting

Print ALL matching words in a string

Hi. str=" {aaID=z_701; time=2012-10-08 00:00:00.000}; {aaID=S_300; time=2012-10-08 00:00:00.000}]}; ansokningsunderlag={anmaln......} {aaID=x_500; time=2012-10-08 00:00:00.000}]}; ansokningsunderlag={anmaln......}" I want to print: z_701 S_300 x_500 if I use : echo $str | sed -n... (4 Replies)
Discussion started by: freddan25
4 Replies

4. Shell Programming and Scripting

regular expression matching whole words

Hi Consider the file this is a good line when running grep '\b(good|great|excellent)\b' file5 I expect it to match the line but it doesn't... what am i doing wrong?? (ultimately this regex will be in a awk script- just using grep to test it) Thanks, Storms (5 Replies)
Discussion started by: Storms
5 Replies

5. Shell Programming and Scripting

matching group of words

Hi, I am stuck with a problem, will be thankful for your guidance and help. I have two files. Each line is a group of words with first word as group Id. eg. 'gp1' in File1 and 'grp1' in File2. <File1> gp1 : xyz xys3 syt2 ssx itt kty gp2 : syt2 kgk iti op2 gp3 : ppy yt5 itt sky... (11 Replies)
Discussion started by: mira
11 Replies

6. Shell Programming and Scripting

Adding numbers matching with words

Hi All, I have a file which looks like this: abc 1 abc 2 abc 3 abc 4 abc 5 bcd 1 bcd 3 bcd 3 bcd 5 cde 7 This file is just a miniature version of what I really have. Original file is some 1 million lines long. I have tried to come up with the code for what I wish to accomplish... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

Matching words based on column headers

Hi , Pls help on this. Input file: NAME1 BSC1 TEXT ID 1 MAINSFAIL TEXT ID 2 DGON TEXT ID 3 lOADONDG NAME2 BSC2 TEXT ID 1 DGON TEXT ID 3 lOADONG (1 Reply)
Discussion started by: bha148
1 Replies

8. Shell Programming and Scripting

Matching words in Perl

Hi, I have an array in which one column can contain any statement. From multiple rows of that column I want to match the statement like "Execution Started." If that row contains "Execution started." then only I have to fetch other data of other columns of that particular row. I dont want... (2 Replies)
Discussion started by: monika
2 Replies

9. UNIX for Advanced & Expert Users

matching words using regular expressions

following file is taken as input aaa bbb ccc ddd eee ffff grep -w aaa <filename> gives proper output. grep \<\(aaa\).*\> filename :- should give output, since aaa is at begining, however i dosen't get any ouput. Any discrepancy. machine details:- Linux anaconda... (1 Reply)
Discussion started by: bishweshwar
1 Replies

10. Programming

getting file words as pattern matching

Sir, I want to check for the repation of a user address in a file i used || as my delimiter and want to check repetaip0n of the address that is mailid and then i have to use IMAP and all. How can i do this... I am in linux ...and my file is linux file. ... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies
Login or Register to Ask a Question