I have a file uca.xml (Thunar configuration for custom actions):<?xml encoding=UTF-8 version=1.0?><actions><action>    <!--some code--></action></actions><--blank line here-->Mind that the file ends with a blank line I want a bash command/script to insert a file customAction.txt containing:<action>    <!--custom configuration--></action>so it will end looking like:<?xml encoding=UTF-8 version=1.0?><actions><action>    <!--some code--></action><action>    <!--custom configuration--></action></actions>I tried a method given by jfgagne:sed -n -i -e '/<\/actions>/r customAction.txt' -e 1x -e '2,${x;p}' -e '${x;p}' uca.xmlbut it works only if there is at least one character (not a blank line) inserted below the  tag.My temporary workaround is a script:echo someDummyText >> uca.xmlsed -n -i -e '/<\/actions>/r customaction.txt' -e 1x -e '2,${x;p}' -e '${x;p}' uca.xmlsed -i 's/someDummyText//' uca.xmlsed -i '${/^$/d;}' uca.xmlbut I believe there is a more elegant, one-line solution with sed. Thanks in advance.Note: this is my very post to the U&L and StackExchange community, so please be lenient with me and do not hesitate to correct me verbosely if I did something wrong. I read the SE FAQ and tried to search for an answer elsewhere with not much luck.
sed cannot insert if a file ends with empty line
shell;text processing;sed
I think your solution is perfectly adequate.  You can optimise it a bit by replacing the last 2 seds with just one:sed -i '$d' uca.xmlAfter all, you just put the extra line there, so you know what you are removing.If you are looking for alternative solutions, you can use sed to get the line number of the pattern match, and then use that -1 to do the read:if lno=$(sed -n -e '/<\/actions>/=' uca.xml)then let lno=lno-1     sed  -i -e $lno'r customaction.txt' uca.xmlfior if you like the ancient but serviceable ed command you can doed -s uca.xml <<\!?</actions>-1r customaction.txtwq!