Reputation: 13
I'm trying to write 3 lines at the beginning of my file using sed. I've decided to assign these to variables because they're pretty long texts and it would be easier to read.
I'm trying to add:
<?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'>
I've assigned each one to: SED_HELPER_ONE SED_HELPER_TWO SED_HELPER_THREE
respectively. Also, is it possible to use variables in the sed command?
Upvotes: 0
Views: 82
Reputation: 247210
If you insist on 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
If it works as you want, add the -i
option to save in-place.
Upvotes: 2
Reputation: 532418
I wouldn't even use sed
for this. Instead, use cat
cat - foo.xml > tmp <<EOF && mv tmp foo.xml
<?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'>
EOF
Upvotes: 2