在每行开头添加前缀字符串 [英] Add a prefix string to beginning of each line

查看:118
本文介绍了在每行开头添加前缀字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下文件:

line1
line2
line3

我想得到:

prefixline1
prefixline2
prefixline3

我可以编写Ruby脚本,但是如果不需要的话,更好.

I could write a Ruby script, but it is better if I do not need to.

prefix将包含/.这是一条路径,例如/opt/workdir/.

prefix will contain /. It is a path, /opt/workdir/ for example.

推荐答案

# 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命令变为

If prefix contains /, you can use any other character not in prefix, or escape the /, so the sed command becomes

's#^#/opt/workdir#'
# or
's/^/\/opt\/workdir/'

这篇关于在每行开头添加前缀字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆