Bash 全局匹配和替换多行文本

Bash 全局匹配和替换多行文本,bash,ubuntu,awk,sed,grep,Bash,Ubuntu,Awk,Sed,Grep,我正在处理一个包含许多文件(100+)的项目 我想更新的每个页面上都有两个FE属性。比如说, property1: { type: String, notify: true, value: "PropOne" }, property2: { t

我正在处理一个包含许多文件(100+)的项目

我想更新的每个页面上都有两个FE属性。比如说,

                property1: {
                    type: String,
                    notify: true,
                    value: "PropOne"
                },
                property2: {
                    type: String,
                    notify: true,
                    value: "PropTwo"
                },
我可以分别对“PropOne”和“Prop2”进行grep,但为了更精确,我宁愿匹配整个property对象

有没有一种方法可以使用GREP之类的工具查找和更新值

编辑:预期输出为:

                property1: {
                    type: String,
                    notify: true,
                    value: "this is new PropOne"
                },
                property2: {
                    type: String,
                    notify: true,
                    value: "this is new PropTwo"
                },

以上使用GNU awk进行多字符RS和ARGID。必要时,POSIX版本并不难实现。它使用字符串比较和替换,因此文件中的regexp和/或反向引用元字符将被视为文本,因此不需要任何特殊考虑(与尝试使用sed或任何其他基于regexp的方法不同)


以上使用GNU awk进行多字符RS和ARGID。必要时,POSIX版本并不难实现。它使用字符串比较和替换,因此文件中的regexp和/或反向引用元字符将被视为文本,因此不需要任何特殊考虑(与尝试使用sed或任何其他基于regexp的方法不同)。

下面是另一个
gnu awk
解决方案:

awk -v RS='},' 'NF {
   sub(/value: "[^"]*"/, "\"this is new " ($1=="property1:"?"PropOne":"PropTwo") "\"")
}
{
   ORS=RT
} 1' file
输出:

               property1: {
                    type: String,
                    notify: true,
                    "this is new PropOne"
                },
                property2: {
                    type: String,
                    notify: true,
                    "this is new PropTwo"
                },

下面是另一个
gnu awk
解决方案:

awk -v RS='},' 'NF {
   sub(/value: "[^"]*"/, "\"this is new " ($1=="property1:"?"PropOne":"PropTwo") "\"")
}
{
   ORS=RT
} 1' file
输出:

               property1: {
                    type: String,
                    notify: true,
                    "this is new PropOne"
                },
                property2: {
                    type: String,
                    notify: true,
                    "this is new PropTwo"
                },
这可能适用于您(GNU-sed):

这将在一系列行上匹配,这些行以
属性开始,以
},
结束,然后用所需的替换项替换双引号之间的字符串(如果需要,使用反向引用)。

这可能适用于您(GNU-sed):


这在一系列行上匹配,这些行以
属性开始,以
},
结束,然后用所需的替换替换替换双引号之间的字符串(如果需要,使用反向引用)。

预期输出是什么?我想以属性1结束:{type:String,notify:true,value:“这是新的PropOne”},property2:{type:String,notify:true,value:“这是新的PropTwo”},而不是在注释中,更新问题。预期的输出是什么?我希望以property1:{type:String,notify:true,value:“this is the new PropOne”},property2:{type:String,notify:true,value:“这是新的PropTwo”},不在注释中,更新问题。
sed -i '/^\s*property.*: {/,/^\s*},/s/"\(.*\)"/"this is the new &"/' file