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

后端 未结 7 2009
轮回少年
轮回少年 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

    Instead of the for loop, it is more appropriate to use while read...:

    while read -r line; do
    do
        echo "$line" | sed -e 's/^/prefix/'
    done < $file
    

    But you would be much better off with the simpler:

    sed -e 's/^/prefix/' $file
    
    0 讨论(0)
提交回复
热议问题