用bash脚本中的新行替换字符串

时间:2019-05-14 18:46:25

标签: linux bash sh

我想获得以下格式的输出:

# JENKIKNS start ===
<p>abc</p>
<p>def</p> 
<p>ghi</p> 
# JENKINS end ===

但是我明白了:

# JENKIKNS start ===
abc def ghi
# JENKINS end ===

我的代码在这里:

ABC="abc \
def \
ghi \
"

sed -i.orig "
/java/ i\

# JENKIKNS start === \\
"$ABC"\\
# JENKINS end === \\
" sampledeploy.sh

2 个答案:

答案 0 :(得分:0)

如果您希望将\替换为换行符,应该可以使用

sed 's/\\/\\\n/g' test

答案 1 :(得分:0)

你的意思是那样的吗?

sampledeploy.sh | sed '/# JENKIKNS start ===/ {n;s/\(\w*\)/<p>\1<\/p>/g;s/ /\n/g}'

说明

sed 
'/# JENKIKNS start ===/   # search this pattern
{n                        # go to next line
;                         # next command
s/                        # substitute
\(\w*\)                   # save words into arg1 (\1)
/<p>\1<\/p>               # write \1 between tags
/g                        # on hole line
;                         # next command
s/ /\n/                   # replace ' ' with '\n'
g}'                       # on hole line
相关问题