When using pipe with or without global g option gives same result:
echo -e 'hi\nhello\nhi' | sed '1s/hi/hey/g'
hey
hello
hi
works for here-string as well when not using global g option:
sed '1s/hi/hey/' <<< $(echo -e 'hi\nhello\nhi')
hey hello hi
but when using global g option and here-string replaces the pattern at every line:
sed '1s/hi/hey/g' <<< $(echo -e 'hi\nhello\nhi')
hey hello hey
Why this change in the output when using the global substitution flag and when input comes through here-string?
It's not got much to do with the
sed
script and does have a lot to do with thebash
here-string notation and quotes. Usecat
instead ofsed
and you see:So, you can see that with the unquoted here-string, you get three words on one line. The
g
modifier on thesed
command1s/hi/hey/g
means that both occurrences ofhi
on the first line of input are changed. In the absence of theg
modifier, only the first occurrence on the first line is changed. The third line never gets modified because the1
in1s/hi/hey/
limits the changes to the first line of input.This also explains the 3 lines vs 1 line of output.