Reputation: 3052
I want to pipe the stdout of a process through a "tool" to prepend some chars to each line. I'm working in a bash.
Example:
PREPEND=' * '
foo.bin | toolXY "$PREPEND"
If foo.bin will output:
hello
world
the Output after toolXY should be:
* hello
* world
What whould be the command for toolXY
?
Upvotes: 2
Views: 890
Reputation: 1046
Awk would work for this as well.
cat foo.bin | awk 'PREPEND=" * " {print PREPEND $0}'
Upvotes: 2
Reputation: 361585
foo.bin | sed "s/^/$PREPEND/"
or
foo.bin | while IFS= read -r line; do echo "$PREPEND $line"; done
The second is more robust if $PREPEND
can contain unpredictable special characters. IFS=
preserves leading whitespace on each line and -r
protects backslashes from being interpreted as escape sequences.
Upvotes: 2