【发布时间】:2021-05-21 08:10:50
【问题描述】:
我有这个正则表达式,我想与 sed 一起使用。我想使用 sed,因为我想批量处理几千个文件,而我的编辑器不喜欢这样
查找:"some_string":"ab[\s\S\n]+"other_string_
替换:"some_string":"removed text"other_string_
Find 基本上匹配 some_string 和 other_string 之间的所有内容,包括特殊字符,如 , ; - 或 _ 并将其替换为文本已删除的警告。
我正在考虑将字符类 [[:space:]] 和 [[:alnum:]] 结合起来,但没有奏效。
【问题讨论】:
-
我收到一条错误消息,指出 z 是非法选项。这在 Mac 上不起作用吗?
-
所以,你有一个 FreeBSD sed,试试
sed -e '1h;2,$H;$!d;g' -e 's/"some_string":"ab.*"other_string_/"some_string":"removed text"other_string_/g' file。在other_string_之后的同一行 上是否还有其他文字? -
不错。我怎样才能做到这一点?
-
试试:
perl -i -0777 's/(?s)("some_string":)"ab.+"(other_string_)/$1"removed text"$2/g' file