Link to home
Start Free TrialLog in
Avatar of bryanlloydharris
bryanlloydharris

asked on

grep for the first match only

Hi,

If I type this, I can grep through a file and get all matches, and then print only the first ones.

grep -n something somefile | head

But with a file size of 2.0GB, it takes a really long time.  I guess this is because grep has to go all the way through and pass _all_ the data to head?  Anyway, is there a faster way to just have grep put the very first thing it matches without having to pipe to another program?  Specifically, I would like the line number also, because I need just stuff after "04/Feb" in a log file.

Bryan
ASKER CERTIFIED SOLUTION
Avatar of dbardbar
dbardbar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bryanlloydharris
bryanlloydharris

ASKER

Oh I must have missed that -m option from the man pages.... I thought I looked but maybe I am getting sleepy.

Of course, the "04/Feb" is the thing I am searching for.  It is a log file written by apache, so it hopefully has this string in it somewhere.

Bryan
Also, I can explain a bit more...  the log file is at such a large size that the statistics program wusage does not seem to be able to run with this log file as the input.  So what I want to do is find the line number from Feb 04 and then I will know where to split the log file.  The reason Feb 04 is important is because the wusage pages have information all the way up to Feb 3, but then the information stops.  So I just need from Feb 04 onward to today.

Bryan
OK.
I'm sure you have other ways to split the file using the line number. But, I'll just mention there's also a way to do it with grep.

grep -A 999999999 -m 1 "04/Feb" logfile

Although, I'm not sure how efficient this method is.
Ah that's very smart, thanks!

My plan was to do this once I found out about the -m:

grep -n -m 1 "04/Feb" logfile > logfile,new
cp logfile,new logfile
rm logfile,new

But then again yours is much shorter and much less typing.

Bryan