0

I have a folder which is full of *.java files. it has the following method in it:

options.addArguments(//"--no-sandbox", // trying to make browser opening faster, but this argument can have security implications
            "--disable-extensions" // avoid the chrome automation extension crash
            , "--no-proxy-server" // trying to make browser opening faster
            , "--proxy-server='direct://'" // trying to make browser opening faster
            , "--proxy-bypass-list=*" // trying to make browser opening faster
            , "--proxy-server=" // trying to make browser opening faster
            // ,"--blink-settings=imagesEnabled=false" // disable images loading for faster testing
            );

i wanted to change this as following:

options.addArguments("--no-sandbox", // trying to make browser opening faster, but this argument can have security implications
            "--disable-dev-shm-usage"
            , "--disable-extensions" // avoid the chrome automation extension crash
            , "--no-proxy-server" // trying to make browser opening faster
            , "--proxy-server='direct://'" // trying to make browser opening faster
            , "--proxy-bypass-list=*" // trying to make browser opening faster
            , "--proxy-server=" // trying to make browser opening faster
            // ,"--blink-settings=imagesEnabled=false" // disable images loading for faster testing
            );

I have searched in the forum and found some solutions, replce string is what i wanted so tried the following:

grep --null -rl //"--no-sandbox" -l | tr '\n' ' ' | xargs --null sed -i 's///"--no-sandbox"/"--no-sandbox",
            "--disable-dev-shm-usage|,/g'

But it throwing me the following error:

sed: -e expression #1, char 6: unknown option to `s'

I guess the old_string and new_string formates are the problem here. how can I fix it, or do it in a more efficient and meaningful way.

Manoj Kumar
  • 139
  • 7
  • `s/pattern/repl/g` will replace all occurrences of pattern with repl. But `s///pattern/repl/g` is just a syntax error. – William Pursell Sep 20 '21 at 11:24
  • exactly, in my pattern i have //. but, am not sure how to pass it along with string – Manoj Kumar Sep 20 '21 at 19:51
  • `s|pattern|repl|g` will also work. Just change the delimiter. – William Pursell Sep 20 '21 at 19:52
  • The trivial error here is that you have to use a different delimiter in `sed` if you want to replace slashes, or escape the literal slashes. The other basic problem with your attempt is that the quotes are being removed by the shell before `grep` sees them. – tripleee Sep 21 '21 at 05:20
  • @tripleee how can i do that, can you suggest a way – Manoj Kumar Sep 21 '21 at 08:40
  • Do the answers you already got not work for you? https://stackoverflow.com/questions/5864146/using-different-delimiters-in-sed-commands-and-range-addresses shows how to use a different delimiter and https://stackoverflow.com/questions/3420429/how-to-run-shell-script-command-with-double-quotes-in-the-argument explains how to quote quotes. – tripleee Sep 21 '21 at 08:46

1 Answers1

0

If GNU sed is available, how about:

sed -i -zE 's#//("--no-sandbox"[^\n]+\n[[:blank:]]+)#\1"--disable-dev-shm-usage"\n            , #' file.java

The -z option to sed assigns the line delimiter to NUL character which enables to slurp whole lines separated by newline characters.

If you want to perform the same substitution on the *.java files recursively, please try:

find . -type f -name "*.java" -print0 | xargs -0 sed -i -zE 's#//("--no-sandbox"[^\n]+\n[[:blank:]]+)#\1"--disable-dev-shm-usage"\n            , #'

As the -i option forces to overwrite the original files, make sure to backup the files (or put the backup suffix to the -i option) before the test.

tshiono
  • 21,248
  • 2
  • 14
  • 22