5 ms·
Do someone know how to run a command for every line emitted in a unix pipe? I tried the oneliner in the linked story using Mac OS X’s `say`, but it doesn’t work
by yuchi 12y ago
Do someone know how to run a command for every line emitted in a unix pipe? I tried the oneliner in the linked story using Mac OS X’s `say`, but it doesn’t work with pipes.
- sheepz 12y agoping goodhost | sed -l 's/.*/ping/' | while read line; do echo $line | say; done
- n3t 12y agoYou might want to use "--unbuffered" flag in sed execution.
- sheepz 12y agothat option appears to be gnu-specific. -l should make it line-buffered
- masklinn 12y agoNote that -l is also an extension, just a BSD one. The only options in POSIX sed are -e, -f and -n (and -e is usually cargo-culted by people who want -E. -E being a BSD extension enabling extended regular expressions — -r in GNU sed)
- petercooper 12y agoBit of a tangent, but using the voice "Tom" on OS X, saying "ping" says "pling" instead. Fine with other voices but rather curious.
- deleted 12y ago[deleted]
- gpvos 12y agocat input |while read a; do do_something_with $a; done
- yuchi 12y agothank you so much!
- kosma 12y agoIn a typical UNIX fashion, there's a program that runs programs: ping 127.0.0.1 | sed -e 's/.*/ping/' | xargs -d'\n' -n1 say
- masklinn 12y ago-d is a GNU extension, won't work on OSX (short of having installed and running GNU xargs), and it's not needed either in this case. And `sed -e <command>` is redundant, `-e` is used to specify multiple commands.
- masklinn 12y agoxargs -n1 (-n is so it executes the command for each input) You'll also need sed -l to limit buffering. ping <host> | sed -l 's/.*/ping/' | xargs -n1 say
- yuchi 12y agoperfect!
- deleted 12y ago[deleted]
- jnsaff2 12y agoFor some fun: https://github.com/mpdehaan/ansible-and-juliet https://github.com/mpdehaan/ansible-and-juliet