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

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

问题描述

我有一个文件如下:

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天全站免登陆