简体   繁体   中英

Substitute a string with a new line in bash script

I want to get an output in this format:

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

but I get this:

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

My code is here:

ABC="abc \
def \
ghi \
"

sed -i.orig "
/java/ i\

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

如果您正在考虑用换行替换\\,这应该有效

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

You mean something like that?

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

explanation

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM