我有一个文件如下:
line1
line2
line3
我想得到:
prefixline1
prefixline2
prefixline3
我可以写一个 Ruby 脚本,但如果我不需要的话会更好。
prefix
将包含 /
。它是一个路径,例如 /opt/workdir/
。
最佳答案
# If you want to edit the file in-place
sed -i -e 's/^/prefix/' file
# If you want to create a new file
sed -e 's/^/prefix/' file > file.new
如果 prefix
包含 /
,您可以使用任何其他不在 prefix
中的字符,或者
转义 /
,所以 sed
命令变为
's#^#/opt/workdir#'
# or
's/^/\/opt\/workdir/'
https://stackoverflow.com/questions/2099471/