Using sed to remove string or paragraph between delimiter

sedtext processing

I would like to know who to remove a string or paragraph that are between ((( string )))

Lorem ipsum (((dolor sit amet))), consectetur adipiscing elit. Vestibulum aliquet fringilla est, dictum tempor nunc venenatis at. Sed nec velit sit amet velit cursus imperdiet. Vivamus tincidunt ut nunc quis euismod. Quisque sit amet lorem rhoncus, malesuada justo at, ullamcorper erat.

So "dolor sit amet" should not be in the return

Here's the cmd I have for now which detect the first ((( but then stop…

sed -e "/(((/,/)))/d" file.txt

Best Answer

Doing this for single line strings is very simple:

sed 's/((([^)]*)))//g' file

If you need it to deal with multiline strings, it gets more complex. One approach would be to use tr to replace all newlines with the null character (\0), make the substitution and translate back again:

tr '\n' '\0' < file | sed 's/((([^)]*)))//g' | tr '\0' '\n'

Alternatively, you could just use perl:

perl -0pe 's/\(\(\([^)]+\)\)\)//g;' file

The -0 causes perl to read the entire file into memory (this might be a problem for huge files), the -p means "print each line" but because of the -0, the "line" is actually the entire file. The s/// is the same idea as for sed.

Related Question