Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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)

share|improve this question
add comment

2 Answers

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
share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.