0

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.