Fast grep / grep for line number only?

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

I am looking for a quick way to grep a file for matching, and I am also looking for the fastest way to grep a file for matching and only return the line number to the remaining match string. I don’t mind if the syntax is complicated, while its fast, I’m going to use it in the complexity of the program , It's not a problem.

I also need this method to work if I need a regex for a pattern so that I can also search for a range. Therefore, if I need to search for all numbers less than 10, if commmand supports it by default, or if it should be some kind of regular expression, I'm just looking for the fastest method I can find.

thank.

Edit

The files I'm working with will be very large, my test file is 1.9gb

+5
source share
5 answers

I think KingsIndian is on target with a parameter -mfor grep, but if speed is your primary goal, it cutmight be faster than awkfor that specific use. try:

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

-d: cut, , -f1 , .

+5

:

grep -n -m 1 str file | awk -F: '{print $1}'

m , . awk .

5 :

grep -n -m 5 str file | awk -F: '{print $1}'

Edit:
tail . , 5 7: grep -n -m 12 str file| tail -7 | awk -F: '{print $1}'

+3

, , :

nl -b a "<filename>" | grep "<phrase>" | awk '{ print $1 }'
+1

GNU awk :

awk '/regex/ { print NR }' file.txt

, , , 10:

awk '{ for (i=1; i<=NF; i++) if ($i <= 10) print NR }' file.txt

, 10. , . :

awk '{ for (i=1; i<=NF; i++) if ($i <= 10) array[NR]++ } END { for (i in array) print i }' file.txt

, sort -n. (.. ):

awk '{ for (i=1; i<=NF; i++) if ($i <= 10) array[NR]++ } END { for (j in array) sorted[k++]=j+0; n = asort(sorted); for (j=1; j<=n; j++) print sorted[j] }' file.txt

EDIT:

awk, if ($i <= 10) if ($i >= 11 && $i <= 20), 11 20 .

+1

- , 1- , $PATTERN :

(spoiler: grep 5 , awk )

user@box:~$ ls -lh /dev/shm/test 
-rw-r--r-- 1 user user 979M Jul  8 09:50 /dev/shm/test
user@box:~$ sed --version | head -n1
GNU sed-Version 4.2.1
user@box:~$ time sed -n "/$PATTERN/{=;q}" /dev/shm/test
206558

real    0m6.835s
user    0m6.160s
sys 0m0.648s
user@box:~$ grep -V | head -n1
grep (GNU grep) 2.14
user@box:~$ time grep -n -m 1 "$PATTERN" /dev/shm/test | cut -d: -f1
206558

real    0m1.337s
user    0m0.592s
sys 0m0.736s
user @ box : ~ $ awk --version | head -n1
GNU Awk 4.0.1
user @ box : ~ $ time awk "/ $ PATTERN / {print NR}" / dev / shm / test
206558

real 0m7.176s
user 0m6.356s
sys 0m0.776s

+1
source

All Articles