Example:
echo one two three | sed 's/ /\n/g' | sed 's/^/:/g'
output:
:one
:two
:three
Without piping:
echo one two three | sed 's/ /\n/g;s/^/:/g'
output:
:one
two
three
seems like first pattern isn’t expanded before executing second one, but I really don’t know much about sed
How can I use first example without piping twice?
PS Pattern used in examples is informative
The main problem here is that sed decides on what constitutes a line (a pattern that it works on) before executing any commands. That is, if you have only one pattern (
one two three), it won’t get reinterpreted as multiple lines after execution ofs/ /\n/g. If would be still a single pattern, although that would be the one that contains newlines inside it.The simplest workaround to make sed reinterpret patterns along the newly inserted newlines is just running sed twice, as you did.
Another workaround would be adding something like
moption (multi-line buffer) toscommand: