0

I want to inject a sed call in following string from a minified .js file

\n\ttar -xf abc.tar.gz\n\tTAR_EXIT

Right before the \n\tTAR_EXIT I want to inject:

\n\tsed -i 's/if(this._eService.isBuilt)/if(!this._eService.isBuilt)/' abc*/out/logs/main/server.main.js

So the final result looks like

\n\ttar -xf abc.tar.gz\n\tsed -i 's/if(this._eService.isBuilt)/if(!this._eService.isBuilt)/' abc*/out/logs/main/server.main.js\n\tTAR_EXIT

I am on zsh but I can switch to bash as well.

This string is a part of a complete bash script enclosed inside a 1-line javascript file that is fetched from the web, which is why I am looking at using sed to replace the characters.

I have already looked into various posts here regarding mass-escape of regex characters This one so far has come close but the string is still not being matched by sed

The baseline for what I want to achieve looks like this in bash:

sed -i "s|\n\ttar -xf abc.tar.gz\n\tTAR_EXIT|\n\ttar -xf abc.tar.gz\n\tsed -i 's/if(this._eService.isBuilt)/if(!this._eService.isBuilt)/' abc*/out/logs/main/server.main.js\n\tTAR_EXIT|g"

Here I have used | as the delimiter.

I even disabled the history operation using set +H to escape the !'s but this is not enough.

RuMAN S
  • 123
  • 7

1 Answers1

0

This answer https://stackoverflow.com/a/10467453/6437140 by @Gurpartap Singh has solved it for my usecase.

The wrapper script

#!/bin/bash
file="${3:--}"
sed "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g" $file
RuMAN S
  • 123
  • 7