Artificial intelligent assistant

sed: Conditional line deletion in one command Assume I have the following example text in `example.txt`: <para>This is some paragraph text This is another line of paragraph text. </para> What I want to achieve: Replace all `<para>` and `</para>` tags with the null string, if the resulting line is empty, delete the line. I achieved this with the following two commands: # remove the line that solely consist of the para tag. $ sed -i '/^<para>$/d;/^<\/para>$/d' ./example.txt # Replace any of the para tags with an empty string. $ sed -i 's/<para>//g;s/<\/para>//g' ./example.txt My question: Is it possible to achieve this in one sed command, making use of a conditional line deletion?

If you have GNU sed, you could use the `T` command to branch past an empty line delete if the replacement fails:


sed 's/<\/\{0,1\}para>//g;T;/^$/d' example.txt


>
> T label
>
>
> Branch to label only if there have been no successful substitutions since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started.

With non-GNU sed it should be possible to do the same by combining `t` and `b`


sed -e 's/<\/\{0,1\}para>//g;ta;b' -e ':a;/^$/d' example.txt

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 7ae216c7c7be92d7b3168174cd136ea7