Skip to content
Advertisement

linux replace string in files recursively

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement