Bash – How to modify output in bash command pipeline

bashpipe

For example, I got from some command some lines

$ some-command
John
Bob
Lucy

Now I'd like to add chaining command, that modifies output.

$ some-command | other-command
Hi John Bye
Hi Bob Bye
Hi Lucy Bye

How to write other-command? (I'm a novice in bash)

Best Answer

awk

$ some-command | awk '{print "Hi "$1" Bye"}'

sed

$ some-command | sed 's/\(.*\)/Hi \1 Bye/'

Examples

Using awk:

$ echo -e "John\nBob\nLucy" | awk '{print "Hi "$1" Bye"}'
Hi John Bye
Hi Bob Bye
Hi Lucy Bye

Using sed:

$ echo -e "John\nBob\nLucy" | sed 's/\(.*\)/Hi \1 Bye/'
Hi John Bye
Hi Bob Bye
Hi Lucy Bye
Related Question