Sujet : Re: a sed question
De : jpstewart (at) *nospam* personalprojects.net (John-Paul Stewart)
Groupes : comp.unix.shellDate : 18. Dec 2024, 21:12:11
Autres entêtes
Message-ID : <lsgokrFfuscU1@mid.individual.net>
References : 1
User-Agent : Mozilla Thunderbird
On 2024-12-18 2:46 p.m., Salvador Mirzo wrote:
(*) Summary
I wrote a sed script that makes a line replacement after it finds the
right spot. So far so good. Then I added quit command after the
change, but the quit does not seem to take effect---violating my
expectation. I'll appreciate any help on understanding what's going on.
[snip]
So far so good. I decided to try it on longer files and I wanted to see
the change more quickly (without long files scrolling past my terminal),
so I decided to add a /q/ command right after the c commmand. I
thought---it will make sed quit right after making the change, so I can
see it works as desired and then I remove the /q/ and release it to
production. But that did not happen.
[snip]
I failed the exercise I gave myself. Can you help me to understand why
the q command isn't stopping sed as I thought it would? I'd like to get
a better intuition.
By default sed prints the "pattern space", i.e. all lines in the file.
You can suppress that with the "-n" option to sed. (In other words, use
"sed -n" instead of plain "sed" in your script.)
The man page for GNU sed 4.9 says about the "q" command:
"Immediately quit the sed script without processing any more input,
except that if auto-print is not disabled the current pattern space will
be printed."
So the behaviour you're seeing without the "-n" option is to be
expected: pattern space still gets auto-printed after the "q" command.