Sed替换条件

时间:2017-05-11 09:04:55

标签: sed

我有以下文本文件。

2017-03-01 10:57:50,892 [Thread-977] limits.compiler : ERROR - Error in formula Undefined_CountryDom
cannot get field LOCKS_CountryDom, String, CountryDom, belongs to Header, scalar (dynamic index: 172)
limits.compiler.LimitsVariablesException: cannot get field LOCKS_CountryDom, String, CountryDom, belongs to Header, scalar (dynamic index: 172)
        at limits.compiler.ExpressionHandler.evaluateBoolean(ExpressionHandler.java:170)
        at limits.compiler.ExpressionHandler.getBoolean(ExpressionHandler.java:266)
2017-03-01 10:57:50,700 [Thread-231] console : ERROR -  at limits.compiler.ExpressionHandler.getString(ExpressionHandler.java:700)
2017-03-01 10:57:50,892 [Thread-977] console : ERROR -  at limits.compiler.compliance.ComplianceCheckFactoryImpl.compileDefaultMessageExpression(ComplianceCheckFactoryImpl.java:107)
2017-03-01 10:57:50,892 [Thread-564] console : ERROR -  at limits.compiler.compliance.ComplianceCheckFactoryImpl.createOverflow(ComplianceCheckFactoryImpl.java:231)
2017-03-01 10:57:50,893 [Thread-977] console : ERROR -  at limits.compiler.compliance.ComplianceCheckFactoryImpl.evaluateTickLockCombinations(ComplianceCheckFactoryImpl.java:498)
2017-03-01 10:57:50,893 [Thread-977] console : ERROR -  at limits.engine.stream.TickWriterImpl.doMLCOperations(TickWriterImpl.java:2488)

我要求删除2017-03-01 10:57:50,700 [Thread-231] console : ERROR - ,以便时间和- at的行与没有上述时间的行相似。

结果应该是这样的:

2017-03-01 10:57:50,892 [Thread-977] limits.compiler : ERROR - Error in formula Undefined_CountryDom
cannot get field LOCKS_CountryDom, String, CountryDom, belongs to Header, scalar (dynamic index: 172)
limits.compiler.LimitsVariablesException: cannot get field LOCKS_CountryDom, String, CountryDom, belongs to Header, scalar (dynamic index: 172)
        at limits.compiler.ExpressionHandler.evaluateBoolean(ExpressionHandler.java:170)
        at limits.compiler.ExpressionHandler.getBoolean(ExpressionHandler.java:266)
        at limits.compiler.ExpressionHandler.getString(ExpressionHandler.java:700)
        at limits.compiler.compliance.ComplianceCheckFactoryImpl.compileDefaultMessageExpression(ComplianceCheckFactoryImpl.java:107)
        at limits.compiler.compliance.ComplianceCheckFactoryImpl.createOverflow(ComplianceCheckFactoryImpl.java:231)
        at limits.compiler.compliance.ComplianceCheckFactoryImpl.evaluateTickLockCombinations(ComplianceCheckFactoryImpl.java:498)
        at limits.engine.stream.TickWriterImpl.doMLCOperations(TickWriterImpl.java:2488)

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以在sed着名的替换s函数
s/regex/pattern/中对正则表达式进行分组。
在这种情况下,我们使用两个组\(a_regex_group\)并打印一个模式,第二个\2

sed 's/\(^[0-9]*.*-\s\s\)\(.*$\)/\t\2/'

这会删除以数字^[0-9]*开头的所有内容,后跟包含短划线和两个空格.*的任意字符-\s\s,并将其余\2留给前导制表符{ {1}}。