0

I use the following command to find under /var some param in my script

grep -R "param" /var/* 2>/dev/null |grep -wq "param"

my problem is that: after grep find the param in file grep continue to search until all searches under /var/* will completed

How to perform stop immediately after grep match the param word

For example when I run the: grep -R "param" /var/* 2>/dev/null |grep -wq "param"

grep find the param after one second.

But grep continue to Search other same param on other files and its take almost 30 seconds

How to stop the grep immediately after param match?

THX

yael
  • 2,765
  • 10
  • 40
  • 48

4 Answers4

1

you can use grep -m 1 to stop after the first match

Leom Burke
  • 8,143
  • 1
  • 35
  • 30
  • 1
    -m 1 will stop after the first match in each file searched. – Leom Burke Jun 17 '10 at 10:27
  • because I try on some options and its not stop the grep search? – yael Jun 17 '10 at 10:29
  • grep -R -m 1 "param" /var/* 2>/dev/null |grep -wq "param" will still return every file that matches but will move on to the next file when it finds the first match. This speeds things up a fair bit. lacopo's answer below will return the first match. – Leom Burke Jun 17 '10 at 10:30
  • yes but my quastion was about to match on the first try and stop not to cont to other files how can I do that? – yael Jun 17 '10 at 11:03
0

This way:

grep -R "param" /var/* 2>/dev/null |grep -wq "param" | head -n 1

Iacopo
  • 4,224
  • 1
  • 23
  • 25
  • hi sorry but this not stop the grep procedure after the first match of param Yael – yael Jun 17 '10 at 11:08
  • That's because "grep" doesn't know that "head" has closed the pipe until it tries to write the second match. – tokland Jun 17 '10 at 11:19
  • I'm tempted to delete the answer, but I think my mistake is in some way instructing – Iacopo Jun 17 '10 at 11:40
  • Yeah, just leave it. Using pipes+head raises some questions, not all commands have a -m option like grep. See: http://www.unix.com/shell-programming-scripting/133120-problem-pipes-infinite-streams.html – tokland Jun 17 '10 at 11:58
  • ( /path/to/slow command with options ) & sleep 5 ; kill $! this not good for me because I check the $? of the grep what can I to do, seems I have abig problem -:( – yael Jun 17 '10 at 12:06
  • Try this: `for i in \`grep -l -s -R param /var/*\`; do grep -m 1 param $i; break;done` – Iacopo Jun 17 '10 at 12:42
  • Check this out to kill grep immediately after first hit: http://superuser.com/a/275962/37154 So, in this case, `head -n 1 <( exec grep -sRw -m1 "linux" /var ); kill $!`. You can wrap this in `()` and grep its output to get the correct RC whether the first `grep` found it or not. – clacke Dec 17 '12 at 10:03
0

If you are using GNU grep, try this:

  -m, --max-count=NUM       stop after NUM matches
Randy Proctor
  • 7,396
  • 3
  • 25
  • 26
0

grep has a -m, --max-count option:

$ grep -R "param" /var/* 2>/dev/null |grep -wq -m1 "param"

Questions: why the double grep? why try to expand with * if you are making grep recursive? consider also -s to silent annoying error messages (I'd recommend alto to use -n to print matching line, but for the -q it seems you want no output):

$ grep -qsRw -m1 "param" /var
tokland
  • 66,169
  • 13
  • 144
  • 170
  • THX the grep -qsRw -m1 "param" /var is very good for my case But I have other quastion how to add time out to grep for example after 20 second I need to break from grep or command how to do that according my first quastion? Yael – yael Jun 17 '10 at 11:25
  • Of course grep does not manage timeouts, this is an orthogonal issue. See for example: http://stackoverflow.com/questions/687948/timeout-a-command-in-bash-without-unnecessary-delay – tokland Jun 17 '10 at 12:01
  • ( /path/to/slow command with options ) & sleep 5 ; kill $! this not good for me because I check the $? of the grep what can I to do, seems I have abig problem -:( – yael Jun 17 '10 at 12:06
  • Yael, this command is part of *the question*, he does not like it so he asks for alternatives. There are plenty of good answers there. For example you can use "timeout" (coreutils package): : timeout 2 mycommand – tokland Jun 17 '10 at 12:27
  • maybe other solution for example to see which proccess is grep and then to kill it? – yael Jun 17 '10 at 12:43
  • there are many ways of killing a process by time, but if _timeout_ is available I don't see why not to use it. – tokland Jun 28 '10 at 16:04
  • Nice to see the complexity reduction in this answer. Will upvote with clearer detail on flags used not explained in answer. – vhs Jul 01 '17 at 10:34