For example, I got from some command some lines
$ some-command
John
Bob
Lucy
Run Code Online (Sandbox Code Playgroud)
Now I'd like to add chaining command, that modifies output.
$ some-command | other-command
Hi John Bye
Hi Bob Bye
Hi Lucy Bye
Run Code Online (Sandbox Code Playgroud)
How to write other-command
? (I'm a novice in bash)
slm*_*slm 16
awk
$ some-command | awk '{print "Hi "$1" Bye"}'
Run Code Online (Sandbox Code Playgroud)
sed
$ some-command | sed 's/\(.*\)/Hi \1 Bye/'
Run Code Online (Sandbox Code Playgroud)
Using awk
:
$ echo -e "John\nBob\nLucy" | awk '{print "Hi "$1" Bye"}'
Hi John Bye
Hi Bob Bye
Hi Lucy Bye
Run Code Online (Sandbox Code Playgroud)
Using sed
:
$ echo -e "John\nBob\nLucy" | sed 's/\(.*\)/Hi \1 Bye/'
Hi John Bye
Hi Bob Bye
Hi Lucy Bye
Run Code Online (Sandbox Code Playgroud)
The code below reads line after line, storing it in variable LINE
. Inside the loop, each line is written back to the standard output, with the addition of "Hi" and "Bye"
#!/bin/bash
while read LINE ; do
echo "Hi $LINE Bye"
done
Run Code Online (Sandbox Code Playgroud)