18

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
fedorqui
  • 252,262
  • 96
  • 511
  • 570
user3922684
  • 183
  • 1
  • 1
  • 4
  • possible duplicate of [Add a prefix string to beginning of each line](http://stackoverflow.com/questions/2099471/add-a-prefix-string-to-beginning-of-each-line) – fedorqui Nov 06 '14 at 16:20

2 Answers2

22

You can do this:

ls -a | grep "filename" | perl -ne 'print "test $_"'
ErikR
  • 50,987
  • 8
  • 69
  • 122
11

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 /'

davemyron
  • 2,323
  • 3
  • 21
  • 32