How can I prepend a string to the beginning of each line in a file?

后端 未结 7 2092
轮回少年
轮回少年 2020-12-08 15:16

I have the following bash code which loops through a text file, line by line .. im trying to prefix the work \'prefix\' to each line but instead am getting this error:

7条回答
  •  感动是毒
    2020-12-08 16:12

    A Perl way to do it would be:

    perl -p -e's/^/prefix' filename
    

    or

    perl -p -e'$_ = "prefix $_"' filename
    

    In either case, that reads from filename and prints the prefixed lines to STDOUT.

    If you add a -i flag, then Perl will modify the file in place. You can also specify multiple filenames and Perl will magically do all of them.

提交回复
热议问题