If I call bash like this: bash|sed 's/a/b/g'
every time bash outputs an 'a' it is replaced with a 'b'
$ bash|sed 's/a/b/g'
$ echo aaa
bbb
$ exit
However if I add additional substitutions like so:
bash|sed 's/a/b/g'|sed 's/c/d/g'`
any output only appears after I leave the bash call:
$ bash|sed 's/a/b/g'|sed 's/c/d/g'
$ echo a
$ echo c
$ exit
b
d
Is there a way to have the case with two pipes behave like the one with one pipe? Or is there a way to do multiple stream substitutions in one call? Also why is it behaving differently in the first place?
Why would anyone do this?
I wanted to write a bash call, that would automatically redact ip-addresses like so:
bash|sed -r 's/([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?/███.███.███.███/g'
and it works great, but if I want to do additional substitutions, for instance for ip-v6, then it no longer works.
This is what I tried
bash|sed -r 's/([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?/███.███.███.███/g' | sed -r 's/([0-9a-f]{0,4}:){5,7}[0-9a-f]{0,4}(\/[0-9]{1,2})?/████:████:████:████:████:████:████:████:████/g'
sed
? Why not usesed -e 's/.../.../' -e 's/.../.../'
? – Kusalananda May 18 '22 at 20:01sed
has a transliteration command (similar to commandtr
). Sosed -e 'y/ab/cd/'
changes a->c, b->d globally. – Paul_Pedant May 19 '22 at 08:36