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?

1

2 Answers 2

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.

0
-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'
2
  • The double quotes within single quotes are literal and the variables won't be interpolated like this. Jan 25, 2018 at 15:03
  • Thanks not working
    – ale8530
    Jan 25, 2018 at 15:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.