【问题标题】:Issue with sed / regex expressionsed / 正则表达式问题
【发布时间】:2020-03-06 17:01:51
【问题描述】:

早上好。我是 REGEX 的新手,所以我正在轻而易举地成功地克服它。

我需要从一个文件中删除一个字符串,它遵循一个模式。在 RegExr 中,字符串工作正常,但是当我运行命令时,它会删除模式,但 有时 也会删除该行的其余部分。

我需要做的是从日志文件中删除“,T = 3054626560”。 3054..... 在整个文件中有所不同。

到目前为止,我的表情是这样的—— sed -i -r 's/(, T =).+?(([0-9]\w+))//g' logfile.log

输入数据示例-

    03/06/2020 10:28:59.885 (27044, T = 3054626560) [D] [INTERFACE] Response Str: {"Status":0}
    03/06/2020 10:29:04.490 (27044, T = 3054626560) [D] [INTERFACE] Parsed Cmd Str: {
       "ServerCmdRequest" : {
          "CmdId" : 529
       }
    }
    03/06/2020 10:29:04.492 (27044, T = 3054626560) [D] [INTERFACE] Response Str: {"ResponseData":{"NeedAuthList":[{"dataSourceName":"UA_SYSTEM_TABLES","index":0,"isSystemTableDB":true,"needAuthentication":false},{"dataSourceName":"ABC","index":1,"isSystemTableDB":false,"needAuthentication":false}]},"Status":0}
    03/06/2020 10:29:05.228 (27044, T = 3054626560) [D] [INTERFACE] Parsed Cmd Str: {
       "ServerCmdRequest" : {
          "CmdId" : 17,
          "CmdInputs" : {
             "isTestRun" : true
          },
          "IDList" : [ 63 ]
       }
    }
    03/06/2020 10:29:05.229 (27044, T = 3054626560) [D] [CELL ACC]  SCell Created: [0x7fe7b0046220]
    03/06/2020 10:29:05.231 (27044, T = 3054626560) [D] [CELL ACC]  SCell Destroyed: [0x7fe7b0046220]
    03/06/2020 10:29:05.232 (27044, T = 3054626560) [D] [INTERFACE] Response Str: {
       "Status" : 10121,
       "errorText" : "Process run results will be lost."
    }

【问题讨论】:

  • 试试sed -i -r 's/, T = [0-9]+//g' logfile.log
  • 成功了吗????
  • @WiktorStribiżew 对不起,我没有看到你在我面前发布了答案。我已经删除了我的答案。随意发布您的答案

标签: regex sed


【解决方案1】:

根据您的输入文件,我建议遵循以下程序。

cat test.txt | sed 's/, T = [0-9][0-9]*//g'

如果您想重写原始文件,您可以将其输出到临时文件,然后返回到您的日志文件。

示例输出:

    03/06/2020 10:28:59.885 (27044) [D] [INTERFACE] Response Str: {"Status":0}
03/06/2020 10:29:04.490 (27044) [D] [INTERFACE] Parsed Cmd Str: {
   "ServerCmdRequest" : {
      "CmdId" : 529
   }
}
03/06/2020 10:29:04.492 (27044) [D] [INTERFACE] Response Str: {"ResponseData":{"NeedAuthList":[{"dataSourceName":"UA_SYSTEM_TABLES","index":0,"isSystemTableDB":true,"needAuthentication":false},{"dataSourceName":"ABC","index":1,"isSystemTableDB":false,"needAuthentication":false}]},"Status":0}
03/06/2020 10:29:05.228 (27044) [D] [INTERFACE] Parsed Cmd Str: {
   "ServerCmdRequest" : {
      "CmdId" : 17,
      "CmdInputs" : {
         "isTestRun" : true
      },
      "IDList" : [ 63 ]
   }
}
03/06/2020 10:29:05.229 (27044) [D] [CELL ACC]  SCell Created: [0x7fe7b0046220]
03/06/2020 10:29:05.231 (27044) [D] [CELL ACC]  SCell Destroyed: [0x7fe7b0046220]
03/06/2020 10:29:05.232 (27044) [D] [INTERFACE] Response Str: {
   "Status" : 10121,
   "errorText" : "Process run results will be lost."
}

【讨论】:

    猜你喜欢
    • 2016-11-02
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    相关资源
    最近更新 更多