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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep only specific "column" but display whole line 1

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hi folks,

any idea how to accomplish the following:

Let's say I got a list.txt looking something like this:

Code:
0001 A1234 0800
0002 B7365 4000
0003 C3874 0001
0004 D3847 2500
0005 E4027 3900
0006 F3268 0001

Now what I'd like to do is perform a

Code:
cat list.txt | grep 0001

in order to get the following output:
(The line where the first "column" contains the 0001)

Code:
0001 A1234 0800

However using the above statement will give me back the following output:

Code:
0001 A1234 0800
0003 C3874 0001
0006 F3268 0001

Because of all those 0001 contained in the third "column".

In what way would I have to change my grep to get rid of this problem ?

Best Regards
Thomas
 
Hi

Just anchor the expression to the beginning of the string : [tt]grep [green]'^0001'[/green][/tt] . But that would also find 00012 in the first column, so either include the following space too : [tt]grep [green]'^0001 '[/green][/tt] or ask it to match only whole words : [tt]grep -w [green]'^0001'[/green][/tt] ( or --word-regexp, if your [tt]grep[/tt] supports it ).

But if there are more than three columns and you have to search in other than first or last column, then better use [tt]awk[/tt] : [tt]awk [green]'$1=="0001"'[/green][/tt] .

Feherke.
 
Anyway, why use the cat command ?
Code:
grep '^0001 ' list.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top