powerpete
powerpete

Reputation: 3052

pipe stdout and prepend some chars for each line

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

Answers (2)

The Dude
The Dude

Reputation: 1046

Awk would work for this as well.

cat foo.bin | awk 'PREPEND=" * " {print PREPEND $0}'

Upvotes: 2

John Kugelman
John Kugelman

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

Related Questions