0

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'
schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57
bananabook
  • 62
  • 5
  • 1
    Why do you chain two _separate_ calls to `sed`? Why not use `sed -e 's/.../.../' -e 's/.../.../'`? – Kusalananda May 18 '22 at 20:01
  • 1
    Does this solve your issue? [sed not working correctly when piped](https://unix.stackexchange.com/q/482272) – Kusalananda May 18 '22 at 20:02
  • Wow, those are both perfect answers, thank you. – bananabook May 18 '22 at 20:12
  • 1
    See [Turn off buffering in pipe](https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe). The issue is that by default, most programs turn on output buffering to get better throughput when the output goes to something other than a terminal, on the assumption that in a pipe or a file, there's no user to need the output right away. – ilkkachu May 18 '22 at 20:17
  • Ok, wow I leaned a lot, thank you. I was not aware, that I stumbled across such a fundamental dilemma. – bananabook May 18 '22 at 20:25
  • Nothing to do with your pipeline problem, but: `sed` has a transliteration command (similar to command `tr`). So `sed -e 'y/ab/cd/'` changes a->c, b->d globally. – Paul_Pedant May 19 '22 at 08:36

0 Answers0