tags:

views:

50

answers:

4

hi friends 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

+1  A: 

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

Leom Burke
sorry but this option not work?Yael
yael
-m 1 will stop after the first match in each file searched.
Leom Burke
can you please give example on my commandTHX
yael
because I try on some options and its not stop the grep search?
yael
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
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
A: 

This way:

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

Iacopo
hisorry but this not stop the grep procedure after the first match of param Yael
yael
That's because "grep" doesn't know that "head" has closed the pipe until it tries to write the second match.
tokland
I'm tempted to delete the answer, but I think my mistake is in some way instructing
Iacopo
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
( /path/to/slow command with options ) kill $! this not good for me because I check the $? of the grepwhat can I to do, seems I have abig problem -:(
yael
Try this: `for i in \`grep -l -s -R param /var/*\`; do grep -m 1 param $i; break;done`
Iacopo
A: 

If you are using GNU grep, try this:

  -m, --max-count=NUM       stop after NUM matches
Randy Proctor
A: 

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
THX the grep -qsRw -m1 "param" /var is very good for my caseBut I have other quastionhow to add time out to grep for example after 20 second I need to break from grep or commandhow to do that according my first quastion?Yael
yael
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
( /path/to/slow command with options ) kill $! this not good for me because I check the $? of the grep what can I to do, seems I have abig problem -:(
yael
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
maybe other solutionfor example to see which proccess is grep and then to kill it?
yael
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