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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
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.
ASKER
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
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
ASKER
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