将sed应用于多个文件

时间:2018-01-25 14:47:19

标签: bash

我有很多文件夹patch_data.conf

我创建了这个脚本:

#!/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' {} \;

启动此命令时,我得到此输出:

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'

我该如何解决?

2 个答案:

答案 0 :(得分:0)

使用:

,

注意" /"而不是" {}",双引号和" 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\";" "在sed调用中的变量扩展中。

修改

为了使其与您的数据一起使用,需要在字符串中添加空格键(在" include"之后):

#!/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" {} \;

<强> EDIT2

完整脚本:

./MIUI_developer/patch_data.conf

include "/home/miui-tool/Patches_jbart/MIUI_9/patches/Weather/Weather.ptch";文件的关键行:

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

运行脚本后,创建文件的差异为:

{{1}}

因此,您会看到该行已删除,并且会引入您想要的替换。

答案 1 :(得分:-1)

shell处理变量扩展。当您使用单引号时,shell会逐字地解释内容。要在执行sed之前让shell扩展变量,您需要将变量用双引号括起来。

protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
  TextBox1.Text = DropDownList1.SelectedItem.Text
}