【问题标题】:finding string has " | , characters with sed command用 sed 命令查找字符串有 " | ,字符
【发布时间】:2017-11-29 17:40:55
【问题描述】:

我的文件夹中有一个字符串模式如下所示的文件

some other strings , "last_fico_high|679", "last_fico_low|675", continues...

想从我的文件中查找并删除这个表达式

"last_fico_high|can_be_any_number",

带有引号、竖线和最后的逗号

我已经尝试过在线正则表达式编辑器,它们看起来不错我可以使用这个表达式找到模式

("last_fico_high\|\d{0,}",)

但是当我用 sed 命令尝试该模式时

sed -i -- 's/"last_fico_high\|\d{0,}",//g' *

我能做的最好的字符串是

|679"

我缺少什么,或者正则表达式应该是什么样的?

【问题讨论】:

    标签: regex sed terminal


    【解决方案1】:

    这个sed 应该适合你:

    s='some other strings , "last_fico_high|679", "last_fico_low|675", continues...'
    
    sed 's/"last_fico_high|[0-9]*",//g' <<< "$s"
    
    some other strings ,  "last_fico_low|675", continues...
    

    在默认的 BRE 模式下,无需转义管道。

    【讨论】:

    • 谢谢,这行得通!但是有一个问题,为什么 \d{0,} 表达式不和 [0-9]* 做同样的事情?是否有多个正则表达式规则?
    • 是的,BRE 不允许 \d。你可以在sed中使用[0-9]\{0,\}
    猜你喜欢
    • 2020-08-26
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2021-10-28
    • 2021-05-16
    • 1970-01-01
    相关资源
    最近更新 更多