Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast grep / grep for line number only?

Tags:

linux

grep

search

I am looking for some help with grep, or grep like tools. This includes but not limited to grep, egrep, awk, sed, or what ever other tool that is used for searching for matches. But i will just call it grep for the rest of the question.

I am looking for the fast way to grep a file for a match, and i am also looking for the fastest way to grep a file for a match and to only return the line number its on not the rest of the matched line. I dont mind if the syntax is complex as long as its fast, I am going to be using it in a program complexity is not the issue.

I also need this method to work if I need to regex for a pattern so i can also search for a range. So if i need to search for all numbers less than 10 if the commmand supports it by default of or if it needs to be some regex i am just looking for the fastest method that i can find.

thank you.

Edit

The files i am working with will be very large, my test file is 1.9gb

like image 248
WojonsTech Avatar asked Sep 16 '12 23:09

WojonsTech


1 Answers

i think KingsIndian is on target with the -m option to grep, but if speed is your main goal, cut may be faster than awk for this particular usage. try:

grep -n -m 1 regex file | cut -d: -f1

the -d: argument tells cut to use a colon as a field seperator, while the -f1 argument tells it to only output the first field.

like image 185
nullrevolution Avatar answered Oct 27 '22 20:10

nullrevolution