如何在unix中的文件之间添加字符串

时间:2013-11-15 04:19:44

标签: unix sed

档案

AVAIL_CHECK_SECS=30
# we need at least one server
if [ -z "$1" ]; then
usage "Missing hostname parameter(s)"
fi

输出应为

AVAIL_CHECK_SECS=30
# cache a copy of the agent jar for pushing to remote servers
# we need at least one server
if [ -z "$1" ]; then
usage "Missing hostname parameter(s)"
fi

我想在2行之间添加一行,即#cache代理jar的副本以推送到远程服务器

2 个答案:

答案 0 :(得分:0)

sed -e '1a\
# cache a copy of the agent jar for pushing to remote servers' File

这适用于行号。您可以/^AVAIL_CHECK_SECS/替换1,或1a替换/# we need/i,方法是匹配应放置该行的位置之前或之后的行上的模式。

答案 1 :(得分:0)

使用awk即可:

awk 'NR==1 {$0=$0 "\n# cache a copy of the agent jar for pushing to remote servers"} 1' file
AVAIL_CHECK_SECS=30
# cache a copy of the agent jar for pushing to remote servers
# we need at least one server
if [ -z "$1" ]; then
usage "Missing hostname parameter(s)"
fi