user3922684
user3922684

Reputation: 213

Append string on grep multiple results in a single command

I want to append a string on the every line from the grep result.

For example, this command will return several lines:

ls -a | grep "filename"

For example:

filename1
filename2
filename3
filename4

How can I append a string test on each return line using a single command? So that I get this output:

test filename1
test filename2
test filename3
test filename4

Upvotes: 21

Views: 34473

Answers (2)

davemyron
davemyron

Reputation: 2542

An alternative is to use sed (which is specifically a Stream EDitor):

ls -a | grep "filename" | sed 's/^/test /'

or

ls -a *filename* | sed 's/^/test /'

or even

ls -a | sed '/filename/bx;d;:x;s/^/test /'

Upvotes: 15

ErikR
ErikR

Reputation: 52039

You can do this:

ls -a | grep "filename" | perl -ne 'print "test $_"'

Upvotes: 23

Related Questions