Nacho Sarmiento
Nacho Sarmiento

Reputation: 473

Join regular expressions for line ranges in sed

I have the following code that finds all <p> </ p> within the label <TXT_accesp> </TXT_accesp> and delete them. This code does that, it works correctly:

find /home -type f -name "*.html" -exec \
    sed -i '/\<TXT_accesp\>/,/\<\/TXT_accesp\>/s@</\?p>@@g' {} \;

The problem is that I need to add more labels. Now you must delete all <p> </ p> that is within <TXT_accesp> </TXT_accesp> or within <TXT_acceng> </TXT_acceng> but I can not join the regular expression OR, I get an error (that the command does not exist, as if badly done).

find /home -type f -name "*.html" -exec \
    sed -i '/\<TXT_accesp\>/,/\<\/TXT_accesp\>\||\<TXT_acceng\>/,/\<\/TXT_acceng\>/s@</\?p>‌​@@g' {} \;

Upvotes: 0

Views: 302

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753725

sed does not support alternation (OR or ||) between ranges of lines. Some versions support extended regexes with alternation in the regex, but what you need here is two commands to sed with the -e option (for readability):

find /home -type f -name "*.html" -exec \
    sed -i -e '/\<TXT_accesp\>/,/\<\/TXT_accesp\>/s@</\?p>@@g' \
           -e '/\<TXT_acceng\>/,/\<\/TXT_acceng\>/s@</\?p>@@g' \
        {} \;

You can collapse that onto one line; you should not do so.

Upvotes: 3

Related Questions