I'm trying to remove some JavaScript between two patterns. The patterns are:
/* React App Start */
And
/* React App End */
I can successfully remove the JavaScript with the following:
sed -i -e '/\/\* React App Start \*\//,/\/\* React App End \*\//d' views/layouts/index.html
However, this also removes the patterns which I do not want. Therefore, I tried the following, but it only removes some of the JavaScript:
sed -i -e '/\/\* React App Start \*\//,/\/\* React App End \*\//{//!d;}' views/layouts/index.hml
Can anyone help please? I'm on a Mac.
CodePudding user response:
You might have more luck using ed
than sed
for this (Assuming Macs come with it); since it was designed from the start for editing files instead of streams of text like sed
, it lets you do things like seek backwards from an address, which is handy here:
printf "%s\n" '/React App Start/ 1,/React App End/-1d' w | ed -s views/layouts/index.html
will delete files in the range one line after the line matching the first pattern to one line before the line matching the second pattern, and then write the modified file back to disk.
CodePudding user response:
To get this to work, I had to enter a new line before the last pattern.
CodePudding user response:
Command grouping...
sed -iE '/\/\* React App Start \*\//,/\/\* React App End \*\//{
/[/][*] React App [StarEnd] [*][/]/n;
d;
}' views/layouts/index.html