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!

Return first occurrence of GREP only

Status
Not open for further replies.

Petemush

Technical User
Jun 21, 2002
255
GB
I'm pretty sure there isn't an option on grep but can anyone think of a way to only return the first matching line that grep finds?

Cheers
 
I've just noticed that the option -m might be what I'm after but I don't seem to have it with my version of grep. Bugger!
 
Something like this ?
grep 'pattern' /path/to/file | head -1
You may also consider this:
awk '/pattern/{print;exit}' /path/to/file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

the fastest way ...

on cmd line (note the syntax):

sed -n '/pattern/{;p;q;}' filename

using files, put in sed-cmd:

#n
/pattern/{
p
q
}

then enter: sed -f sed-cmd filename


:) guggach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top