【问题标题】:Remove multiple occurrences of a string regex on a file, line by line逐行删除文件中多次出现的字符串正则表达式
【发布时间】:2021-10-16 01:42:58
【问题描述】:

我有一个文件 test.txt:

12-09:30:09:802775 |539----> 116 Bl_LE 502450553  |  <D BeginString="FIX.4.2"  (...) LTPrice="13.21" 9999="bar" CheckSum="145" 12345="xxx"></D>
12-09:30:09:802775 |539----> 116 Bl_LE 502450553  |  <D BeginString="FIX.4.2"  (...) LTPrice="13.21" 1010="foo" CheckSum="145" 65464="xxx"></D>

我正在尝试删除以数字(9999="bar"、1010="foo" 等)开头的所有密钥/对,以使最后一行如下所示:

12-09:30:09:802775 |539----> 116 Bl_LE 502450553  |  <D BeginString="FIX.4.2"  (...) LTPrice="13.21" CheckSum="145"></D>
12-09:30:09:802775 |539----> 116 Bl_LE 502450553  |  <D BeginString="FIX.4.2"  (...) LTPrice="13.21" CheckSum="145"></D>

我尝试使用带有“tr”命令的单行命令,但不知道如何将其组合在一起:

$$ perl -ne 'tr/(\d+="[^"]*")//g' test.txt
Bareword found where operator expected at -e line 1, near "tr/(\d+="[^"]*")//g"
syntax error at -e line 1, next token ???
Execution of -e aborted due to compilation errors.

关于如何实现这一目标的任何想法?

【问题讨论】:

  • tr/// 用于映射单个字符替换。正如下面的答案所示,您希望 s/// 使用正则表达式。

标签: regex perl regex-negation


【解决方案1】:

你可以使用:

perl -pe 's/\h+\d+="[^"]*"//g' test.txt

12-09:30:09:802775 |539----> 116 Bl_LE 502450553  |  <D BeginString="FIX.4.2"  (...) LTPrice="13.21" CheckSum="145"></D>
12-09:30:09:802775 |539----> 116 Bl_LE 502450553  |  <D BeginString="FIX.4.2"  (...) LTPrice="13.21" CheckSum="145"></D>

RexEx 详细信息:

  • \h+:匹配 1 个或多个空格
  • \d+:匹配 1+ 位
  • =:匹配一个=
  • "[^"]*":匹配引用的值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-12
    • 2013-02-03
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多