[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A grep bug or a horrible grep feature? :-)

From: Matthew Woehlke
Subject: Re: A grep bug or a horrible grep feature? :-)
Date: Mon, 13 Oct 2008 17:16:40 -0500
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20080723 Fedora/ Thunderbird/ Mnenhy/

Adem wrote:
Try this and tell me if that's ok:

  mkdir test
  cd test
  man grep >test.txt
  touch t.t
  grep -r "," * >t.t
  ls -l

If there is no man page for grep installed on your system
then copy a text file you have (should be > 16 KB) to test.txt
and try the last 3 steps above.

If grep takes longer than 2 seconds or so then you better press CTRL-C !  :-)

Ok, I see what's happening. I'm leaning toward "surprising feature", as it's not really doing anything you didn't ask it to do. What happens:

- grep is invoked as 'grep -r , test.txt t.t' (note: the '-r' isn't needed)
- grep obediently searches for ',' in test.txt, writing results to stdout
- the system obediently pipes said output to 't.t'
- grep then obediently searches for ',' in t.t, writing results to stdout

...at this point, t.t is non-empty, and grep will (presumably) keep reading from t.t until it gets EOF. Of course, since grep outputs as soon as it finds something (which it will), that gets sent to stdout a.k.a. t.t, which means it is that much longer before you hit EOF, which means t.t gets bigger, which means it is that much longer before you hit EOF, which means... (ad infinum).

Since I'm reasonably confident that all of the above behavior taken in isolation is correct (read until EOF even if file is changing, write output immediately), grep is effectively just doing what you told it to do.

You probably know this already, but you can avoid this behavior by omitting the 'touch' in your example... or, for that matter, writing output to something that results in file being used as output getting processed before the file containing matching lines (e.g. "a.log"). This will work if no files containing matching lines occur in grep's argument list before any+all instances of the name of the file being used for output.

Please do not quote my e-mail address unobfuscated in message bodies.
"NT was a marketing name that stood for New Technology, but it was still an amusing coincidence that WNT was VMS with each letter replaced by the next one."
  -- Jeremy Reimer

reply via email to

[Prev in Thread] Current Thread [Next in Thread]