-1

I have a file like this:

1 = zara
2 = gucci

I want to write a bash script which read two variables and decides to change the number in one of lines to a new number. I have this script for this purpose:

pattern="$num[[:space:]]=[[:space:]].*"
sed -ie 's,'"$pattern"','"$num"',\ =\ '"$newBrandName"',' $fileLocation

Variables num, newBrandName and fileLocation have the right value; I have checked this by echo command.

But when I run the script, I get this error:

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

The output of echo on the SED command is this:

sed -ie s,1[[:space:]]=[[:space:]].*,1\ =\ sign, /root/info.info

Can anyone help me with the regex?

Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131

2 Answers2

1

The form of s is s/regexp/replacement/. You can't have multiple expressions on it. Try this instead:

sed -i -e '/^${num}[[:space:]]*=[[:space:]]*/s|.*|$num = $newBrandName|" "$fileLocation"

It would find the line matching ^${num}[[:space:]]*=[[:space:]]* and replace everything on it with $num = $newBrandName.

You also should separate -i and -e so e would not be interpreted as the backup suffix for -i.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
0

If think it's because you have an extraneous , left :

pattern="$num[[:space:]]=[[:space:]].*"
sed -ie 's,'"$pattern"','"$num"',\ =\ '"$newBrandName"',' $fileLocation
--------------------------------^

You might consider escaping it, or use another character like @.

NoDataFound
  • 11,381
  • 33
  • 59