How do I add a prefix/suffix to each line of a file?

% sed 's/^/file1:/' file1

The most readable and easy to remember one for me to add both prefix and suffix is the following:

$ echo "  foo bar  " | awk '{print "--"$0"++"}'

which outputs

  --  foo bar  ++

Note the whole input including leading/trailing spaces is preserved.


Using AWK, you can add pretty much any prefix:

awk -v PRE='anything:' '{$0=PRE$0; print}' file1

This will work even for fancy non-printable chars.