0

As I'm learning more about UNIX commands I started working with sed at work. Sed's design reads a file in line by line, and executes commands on each line individually.

How does grep process files? I've tried various ways of googling "does grep process line by line" and nothing really concrete shows up.

1
  • grep doesn't process anything by itself. grep used to filter by pattern. Commented Jun 20, 2013 at 14:30

3 Answers 3

6

From Why GNU grep is fast :

Moreover, GNU grep AVOIDS BREAKING THE INPUT INTO LINES. Looking for newlines would slow grep down by a factor of several times, because to find the newlines it would have to look at every byte!

and then

  • Don't look for newlines in the input until after you've found a match.
2
  • +1. In order to process the input line-by-line, use the option --line-buffered. However, the manual says: Use line buffering on output. This can cause a performance penalty.
    – devnull
    Commented Jun 20, 2013 at 14:54
  • Exactly the kind of answer I was looking for!
    – avgvstvs
    Commented Jun 21, 2013 at 13:57
1

EDIT: I will correct myself. It is neither line by line nor full file, its in terms of chunks of data which are placed into the buffer.

More details are here http://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html

0

The regular expression you pass to grep doesn't have any way of specifying newlines (although you can specify matches against the start or end of a line).

So it appears to work line by line, even though actually it may not treat line ends differently to other characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.