grep for the first match only


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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Limiting the number of matches before stoping the search - the -m flag.
Printing the line number - the -n flag.

So, for example,

grep -n -m 5 something somefile

I did not understand your last sentence about "04/Feb". Can you explain in more detail what is the format of the file, and what exactly do you want to do?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bryanlloydharrisAuthor Commented:
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.

bryanlloydharrisAuthor Commented:
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.

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.
bryanlloydharrisAuthor Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.