0

I need to remove strings from a text file that matches a REGEX pattern, using regex101 my pattern match works fine, but when I execute using sed, nothing gets deleted and for some reason the regex is not working:

https://regex101.com/r/oLNrDB/1/

I need to remove all occurrences of all text including newlines between the following 2 strings:

DELIMITER ;;
some text with newlines
DELIMITER ;

The sed command I am using is:

sed '/DELIMITER ;;[\S\s]*?DELIMITER ;/d' myfile.sql;

but the output is identical to the input file, what am I doing wrong ?

crankshaft
  • 2,607
  • 4
  • 45
  • 77
  • 2
    Does this answer your question? [sed multiline delete with pattern](https://stackoverflow.com/questions/37680636/sed-multiline-delete-with-pattern) – Alexander Mashin Oct 22 '20 at 04:24
  • See also: [Why does my regular expression work in X but not in Y?](https://unix.stackexchange.com/questions/119905/why-does-my-regular-expression-work-in-x-but-not-in-y) ... assuming you are using `GNU sed`, you cannot use `\s` or `\S` inside character class.. and non-greedy isn't supported.. plus, `sed` works only one line at a time by default, so you cannot match across lines without some more options/commands.. you can use `perl -0777` for a better fit instead of `sed` – Sundeep Oct 22 '20 at 05:53
  • based on the regex101 sample, https://stackoverflow.com/questions/38972736/how-to-print-lines-between-two-patterns-inclusive-or-exclusive-in-sed-awk-or is a better option instead of regex-only solution – Sundeep Oct 22 '20 at 05:58

1 Answers1

1

The problem here is that sed reads files line-by-line and applies the pattern to each line separately. In your case, this means that the one pattern can't match both the starting and finishing delimiter because no one line contains them both.

The sed way of doing this is to use a range with the delete command, /start pattern/,/end pattern/d, which means delete all lines between the start pattern and end pattern inclusive. For example

$ cat foo.txt
some text before
DELIMITER ;;
some text with newlines
DELIMITER ;
some text after
$ sed '/DELIMITER ;;/,/DELIMITER ;/d' foo.txt
some text before
some text after
Jon
  • 3,573
  • 2
  • 17
  • 24