linux - 在每行的开头添加前缀字符串

我有一个文件如下:

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/

相关文章:

python - 有没有办法在 Python 中使用 PhantomJS?

linux - 如何在 Vimdiff 中展开/折叠差异部分?

python - cron 和 virtualenv

windows - Windows 和 Linux 目录名称中禁止使用哪些字符?

linux - 在 Linux 中自动重复命令

linux - Linux 中是否有任何标准的退出状态代码?

python - 转义正则表达式字符串

python - 如何格式化小数以始终显示 2 个小数位?

linux - 在 Linux 中更改默认 shell

python - 如何从多维数组中提取一列?