逐行删除文件中多次出现的字符串正则表达式

Remove multiple occurrences of a string regex on a file, line by line

我有一个文件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>

我正在尝试删除所有以数字开头的 key/pairs(9999="bar",1010="foo",etc.)以得到这样的最后几行:

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.

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

您可以使用:

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+位数字
  • =:匹配一个=
  • "[^"]*": 匹配引用值