1

I have many folders with file patch_data.conf.

I have create this script:

#!/bin/bash    
Directory=~/MIUI_developer
stringreplace="include "/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch";"
stringnew="#include "/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch";"

sudo find $Directory -type f -name "patch_data.conf" -exec sed -i 's/$stringreplace/$stringnew/g' {} \;

When launch this command I get this output:

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

How can I solve it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ale8530
  • 317
  • 1
  • 4
  • 9

2 Answers2

1

Use:

find $Directory -type f -name "patch_data.conf" -exec sed -i "s,${stringreplace},${stringnew},g" {} \;

note the "," instead of "/", double quotes, and "{}" in variables expansion in the sed call.

EDIT:

In order to make it work with your data one needs to add a spacebar (after "include") in the strings:

stringreplace="include \"/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch\";" stringnew="#include \"/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch\";"

EDIT2:

Full script:

#!/bin/bash    
Directory=./MIUI_developer
stringreplace="include \"/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch\";"
stringnew="#include \"/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch\";"


find $Directory -type f -name "patch_data.conf" -exec sed -i "s,${stringreplace},${stringnew},g" {} \;

crucial line of the the ./MIUI_developer/patch_data.conf file:

include "/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch";

after running the script, the diff of the created file is:

< #include "/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch";
---
> include "/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch";

Thus, you see that the line is not deleted, and the substitution you desired is introduced.

Maciek
  • 762
  • 6
  • 17
-1

The shell handles variable expansion. When you use single quotes the shell interprets the contents literally. To get the shell to expand the variables before executing the sed, you'll need to wrap your variables in double quotes.

sed -i 's/"$stringreplace"/"$stringnew"/g'
Jamie Crosby
  • 152
  • 7