Insert a multiline string with arbitrary special c

2019-07-20 15:15发布

Something like:

cat template.txt | ruby -e 'puts STDIN.read.sub("%placeholder%", IO.read("content.txt"))' > output.txt

Or:

ed template.txt <<EOF
/%placeholder%/d
.r content.txt
w output.txt
EOF

Any alternatives?

标签: bash shell
1条回答
地球回转人心会变
2楼-- · 2019-07-20 15:36
[me@home]$ sed -e '/\$xxx/r content.txt' -e '/\$xxx/d' template.txt 
.,:;-+=_'"`*^?!
.,:;-+=_'"`*^?!
&$%#@|/\()[]{}<>
&$%#@|/\()[]{}<>

The first command searches for $xxx then prints out the contents of content.txt. The second one deletes $xxx.

You can also use a multi-line command, which would be more readable when used in scripts.

sed -e '/\$xxx/{
   r content.txt
   d
}' template.txt
查看更多
登录 后发表回答