Sed Error:Bad Option in Substitution Expression

sed: bad option in substitution expression : bad option in substitution expression

use a different sed separator:

sed -i "s#variable \"vpc\".*#variable \"vpc\" { default = \"$string\" }#g"  var.tf

sed: bad option in substitution expression

  1. You shouldn't have s before the regular expressions specifying the range of lines to process.
  2. The g modifier should be part of the s/// command that replaces the line, not after the }.
  3. The pattern /active_mesh = true/ doesn't match because it only has 1 space before =, but the file has 2 spaces there. You can use /active_mesh *= *true/ to match any number of spaces around the =.
sed -i "/spoke1 = [{]/,/[}]/ { s/active_mesh *= *true/active_mesh = false/g; }" /variables.tf

sed command bad option in substitution expression when replacing back slash

Why use sed at all?

$ echo "${VAR//\//\\/}"
\/host\/test

It looks a bit horrible, but it works fine. See ${parameter/pattern/string} in https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion.


Your problem happens because you're using double quotes (") around your sed code, not single quotes (').

In double quotes, \\ is interpreted as the escape sequence for a single \, so the code that sed ends up seeing is:

$ echo "s/\//\\\//g"
s/\//\\//g
#^ ^ ^

sed reads this as "search for \/ and replace it by \\, with /g as options". This is an error because / is not a valid option.

sed: bad option in substitution expression when trying to replace a string

Use ranges to make sure you get the right one. Here's a quick example you can refine -

$: sed '/spoke1/,/\},/ { s/AWS-UE2-Spoke1-GW/myname/; }' file
variable "aws_spoke_gateways" {
default = {
spoke1 = {
name = "myname"
},
spoke2 = {
name = "AWS-UE2-Spoke1-GW"
}
}
}

If you need more granular control, you can nest the ranges.

$: sed '/variable "aws_spoke_gateways" \{/,/^\},/ {
/spoke1/,/\},/ { s/AWS-UE2-Spoke1-GW/new1/; }
/spoke2/,/\},/ { s/AWS-UE2-Spoke1-GW/new2/; }
}' file
variable "aws_spoke_gateways" {
default = {
spoke1 = {
name = "new1"
},
spoke2 = {
name = "new2"
}
}
}

Note that I added an anchor on the ending brace of the outer wrapper: ^\},

The manual is your friend. Read it all the way through once, you'll save enough hours to make up for it.

  • https://www.gnu.org/software/sed/manual/sed.html

Android busybox sed: bad option in substitution expression

If you need pretty formatting, you'll have to inject newlines and tabs (which pain me in sed); an untidy but otherwise formed equivalent below:

I think you only need to match the insertion point (the penultimate or last line) thus:

cat infile | sed -e "s/<\/permission>/<group gid=\"media_rw\"\/><\/permission>/"

gives

<permission name="android.permission.WRITE_EXTERNAL_STORAGE" >
<group gid="sdcard_r" />
<group gid="sdcard_rw" />
<group gid="media_rw"/></permission>

Since I've always struggled with tabs and newlines in sed, awk'll do it nicely if you have it and you insist on pretty output.

cat infile | awk '/\/permission/ { print "\t<group gid=\"media_rw\"\/>";}
/./ {print $0;}
'

gives the same with a tab and a newline:

<permission name="android.permission.WRITE_EXTERNAL_STORAGE" >
<group gid="sdcard_r" />
<group gid="sdcard_rw" />
<group gid="media_rw"/>
</permission>

Edit: For in-file and against the group WRITE_EXTERNAL_STORAGE tag

I'll presume that the ordering of the key-value set within this block is not important. This will instead use the opening permission tag to perform the insertion, and now using -i on sed to edit the original file. Also switching to pipe separators (avoiding \/\/\/). Finally, using & to put matched text rather than type it out -- assuming your busybox build of sed can do this.

sed -e "s|<permission name=\"android.permission.WRITE_EXTERNAL_STORAGE\" >$|&<group gid=\"media_rw\"/>|" -i infile

The tag you're matching only needs to be unique enough. Added EOL-marker ($) to the match part to replace this only once. The match will not work a second time because the line will have changed.

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

When processing file paths with sed, you can make things easier by using an alternate delimiter that is not a part of the values to be processed. The delimiter can be any character that follows the s command. The forward slash / is often seen in examples, but can be any other character like | or #. This makes it unnecessary to escape the / in the values. For example:

sed -i  "1s|/content/drive/Shared drives/Media Library|${path}|" file.txt


Related Topics



Leave a reply



Submit