Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep exact pattern 3

Status
Not open for further replies.

jdhahbi

Technical User
Oct 7, 2009
24
US
Hi
I am using grep to extract a certain pattern in the example files below. How do I extract pattern 'T1' and not 'T123'?
Code:
cat pattern
id
T1
T23
T55
Code:
cat file
id	name	length
T5	Lypla1	2433
T55	sea3	2668
T123	Tmea1	2671
T23	nrea1	2564
T1	mea1	250
T10	mrty1	22
Code:
grep -f pattern file
id	name	length
T55	sea3	2668
T123	Tmea1	2671
T23	nrea1	2564
T1	mea1	250
T10	mrty1	22

the output should be:
Code:
id	name	length
T1	mea1	250
T23	nrea1	2564
T55	sea3	2668
Thank you for your help
Joseph

 
Hi

Your question is off-topic. Please ask [tt]grep[/tt] related questions in forum822 or forum619 or whatever is appropriate.

Regarding your question with GNU [tt]grep[/tt] you can use the [tt]-w[/tt] ( [tt]--word-regexp[/tt] ) switch to anchor the patterns to word boundaries :
Code:
grep [red]-w[/red] -f pattern file

Feherke.
 
And as you asked in the awk forum:
Code:
awk 'NR==FNR{++p[$1];next}($1 in p)' pattern file

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Or adapt your regular expression to what you really want:

Code:
cat pattern
id
T1[^[:digit:]]
T23
T55
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top