Does sed -n '1s / pattern / substitute / p' run the entire file, or does it exit after line 1?

When sed

executed with a 1-line address (line 1) and replaced with it, example:

$ sed -n '1s/foo/bar/p' <<EOF
foo will be replaced, this is line 1
are these lines still processed?
Hard to check.
EOF

      

Does sed keep reading / parsing the rest of the file, or exits after the named address? I am on a very fast system and the timings look like they are stopping, but without a command q

to be sure.

+3


source to share


1 answer


No, it definitely doesn't stop at my version sed

(GNU sed version 4.2.1).

I created a file with many lines (203118592) by running yes

for a few seconds and then pressing Ctrl-C:

$ yes > file

      

This creates a huge file with a bunch of "y" lines. Then I ran the command sed

to replace "y" with "n" (instead of "foo" with "bar") and it took quite a while:



$ wc -l file
203118592 file
$ time sed -n '1s/y/n/p' file
n

real    0m9.335s
user    0m9.237s
sys     0m0.068s

      

You just haven't created a file large enough to see the effect. :)

Also, just for reference, if I use q

it exits as expected:

$ time sed -n '1s/y/n/p ;q' file
n

real    0m0.002s
user    0m0.000s
sys     0m0.000s

      

+4


source







All Articles