使用sed在文件开头添加多行变量

时间:2019-03-25 18:58:48

标签: bash sh

我正在尝试使用sed在文件的开头写入3行。我决定将它们分配给变量,因为它们是很长的文本,而且更易于阅读。

我要添加:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wo-national-phase-information SYSTEM "wo-national-phase-information-v1-5.dtd">
<wo-national-phase-information country='US' file-name='US-IB-FEB-2019.xml'>

我已将每个人分配给: SED_HELPER_ONE SED_HELPER_TWO SED_HELPER_THREE

分别。另外,可以在sed命令中使用变量吗?

2 个答案:

答案 0 :(得分:2)

我什至不使用library(usmap) library(ggplot2) plot_usmap( data = dat, values = "Loan", lines = "red" ) + scale_fill_continuous( low = "white", high = "red", name = "Loan", label = scales::comma ) + labs(title = "US States", subtitle = "States and loan data") + theme(legend.position = "right") 。而是使用sed

cat

答案 1 :(得分:2)

如果您坚持使用sed:

v1='<?xml version="1.0" encoding="UTF-8"?>'
v2='<!DOCTYPE wo-national-phase-information SYSTEM "wo-national-phase-information-v1-5.dtd">'
v3="<wo-national-phase-information country='US' file-name='US-IB-FEB-2019.xml'>"

sed "1i\\
$v1\\
$v2\\
$v3" file

如果您愿意,可以添加-i选项以就地保存。