-1

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.

Rob H
  • 36
  • 3
  • See: [removing lines between two patterns (not inclusive) with sed](https://stackoverflow.com/q/5071901/3776858) – Cyrus Dec 09 '22 at 18:03
  • @Cyrus I looked that that page.. had no luck with `sed -n -e '/\/\* React App Start \*\//{' -e 'p' -e ':a' -e 'N' -e '/\/\* React App End \*\//!ba' -e 's/.*\n//' -e '}' -e 'p' views/layouts/index.html` – Rob H Dec 09 '22 at 18:09
  • OK, so it runs when i install gsed. However, it still doesn't remove the following which is between the two patterns. ` animation-timing-function: ${0}; animation-iteration-count: infinite; animation-delay: 200ms; } `),ea.rippleVisible,la,550,` – Rob H Dec 09 '22 at 18:37

3 Answers3

0

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.

Shawn
  • 47,241
  • 3
  • 26
  • 60
0

To get this to work, I had to enter a new line before the last pattern.

Rob H
  • 36
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 14 '22 at 13:08
0

Command grouping...

sed -iE '/\/\* React App Start \*\//,/\/\* React App End \*\//{ 
  /[/][*] React App [StarEnd]+ [*][/]/n;
  d;
}' views/layouts/index.html
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36