How can I add a line to a bash script using sed?

Hello I am trying to make a script to edit chrome flags on Mac using a bash script. This script should set max SSL for TLS1.3 on chrome. However, I am having some trouble with sed. This is what my command looks like:

sed -i '.bak' -e 's/{\"browser\".*origin\":\"\"\}/\"browser\":\{\"enabled_labs_experiments\":\[\"ssl-version-max@2\"\],\"last_redirect_origin\":\"\"\}/g' "./Local State"

      

The goal is to add

"enabled_labs_experiments": ["SSL version-max @ 2"]

to that

{"browser": {"last_redirect_origin": ""}

so it looks like this:

{"browser": {"enabled_labs_experiments": ["SSL version-max @ 2"], "last_redirect_origin": ""}

Not sure what is wrong with my team, but any help in reaching this goal is really appreciated or just point me in the right direction, it will help a lot.

Thank!

+3


source to share


3 answers


sed -i '.bak' -e 's|\(\"browser\"\):{\(\".*origin\":\"\"\)}|\1:{\"enabled_labs_experiments\":[\"ssl-version-max@2\"],\2}|'

      



The trick is to use \(

and \)

to define two groups, and to use \1

and \2

in your lookup to represent those groups. BTW, your curly braces don't match your examples ...

+1


source


You just throw back slashes all over the place where they don't belong. Don't do this - find out which characters are metacharacters in regex and backreference-enabled strings. Anything you need:



$ sed 's/\({"browser":\)\(.*origin":""}\)/\1"enabled_labs_experiments":["ssl-version-max@2"],\2/' file
{"browser":"enabled_labs_experiments":["ssl-version-max@2"],{"last_redirect_origin":""}

      

0


source


If you have strings in Bash (versus a file), you can use the regex engine in Bash instead sed

to handle them that way.

Given:

$ s1='"enabled_labs_experiments":["ssl-version-max@2"]'
$ s2='{"browser":{"last_redirect_origin":""}'

      

You can break up at first :{

in s2

the following way:

$ [[ $s2 =~ (^[^:]+:){(.*$) ]] && echo "${BASH_REMATCH[1]}{$s1,${BASH_REMATCH[2]}"
{"browser":{"enabled_labs_experiments":["ssl-version-max@2"],"last_redirect_origin":""}

      

Or, if you want to have the same regex as the other answers:

$ [[ $s2 =~ (^{\"browser\":){(\"last_redirect_origin\":\"\"}$) ]] && echo "${BASH_REMATCH[1]}{$s1,${BASH_REMATCH[2]}"
{"browser":{"enabled_labs_experiments":["ssl-version-max@2"],"last_redirect_origin":""}

      

0


source







All Articles