问题描述
[----------] 7 tests from RouteGenSetup
[ RUN ] RouteGenSetup.AcceptTATTest
[2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,rounds: LLLLLL,pattern: TR
[2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR
[2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1,pattern: TR
[ OK ] RouteGenSetup.AcceptTATTest (0 ms)
[ RUN ] RouteGenSetup.BlockLinksTest
[2021-03-08 22:55:53.937] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,pattern: TR
[2021-03-08 22:55:53.938] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR
我想去掉日期和时间戳(例如,[2021-03-08 22:55:53.937]
)。为此,我尝试使用以下 sed
命令:
sed -i '/^\[2021-/s/^.*\]\ //1' output.txt
但是,时间戳右侧的 [info]
或 [error]
括号也会被删除:
[----------] 7 tests from RouteGenSetup
[ RUN ] RouteGenSetup.AcceptTATTest
TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,pattern: TR
TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR
TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1,pattern: TR
[ OK ] RouteGenSetup.AcceptTATTest (0 ms)
[ RUN ] RouteGenSetup.BlockLinksTest
TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,pattern: TR
当我尝试
sed -i '/^\[2021/s/[^\]]*//' output.txt
[----------] 7 tests from RouteGenSetup
[ RUN ] RouteGenSetup.AcceptTATTest
2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,pattern: TR
2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR
2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1,pattern: TR
[ OK ] RouteGenSetup.AcceptTATTest (0 ms)
[ RUN ] RouteGenSetup.BlockLinksTest
2021-03-08 22:55:53.937] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,pattern: TR
2021-03-08 22:55:53.938] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR
并尝试
sed -i '/^\[2021/s/^[^\]]*\]//' output.txt
不会产生任何变化。我不知道如何解决这个问题。我尝试了一些类似帖子中推荐的东西,但没有成功。
注意:这不是 sed - regex square brackets detection in Linux 的副本,因为这里的问题是不同的:只匹配字符串开头和第一个 ]
+ 空格之间的子字符串如果该行以某个前缀开头。
sed replace a word at a line which begins with a specific pattern using 没有解释如何获取以特殊字符开头的行,然后匹配从 start 到第一个 ]
+ 空格的字符串。
解决方法
你可以使用
sed -i '/^\[2021-/s/^[^]]*] //' file
这里,
-
/^\[2021-/
- 查找以[2021-
开头的行
-
s/^[^]]*] //
- 从字符串的开头到第一个]
匹配除]
之外的任何零个或多个字符,然后是一个空格并删除这些匹配项(因为 RHS 为空).
注意 [^]]
是一个 否定括号表达式,它匹配除 ]
之外的任何字符。由于方括号表达式不支持正则表达式转义序列,]
需要是方括号表达式中的第一个字符,因此不能在方括号表达式中使用 \]
anwhere。
s='[----------] 7 tests from RouteGenSetup
[ RUN ] RouteGenSetup.AcceptTATTest
[2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,rounds: LLLLLL,pattern: TR
[2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR
[2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1,pattern: TR
[ OK ] RouteGenSetup.AcceptTATTest (0 ms)
[ RUN ] RouteGenSetup.BlockLinksTest
[2021-03-08 22:55:53.937] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,pattern: TR
[2021-03-08 22:55:53.938] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR'
sed '/^\[2021-/s/^[^]]*] //' <<< "$s"
输出:
[----------] 7 tests from RouteGenSetup
[ RUN ] RouteGenSetup.AcceptTATTest
[info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,pattern: TR
[info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR
[info] TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1,pattern: TR
[ OK ] RouteGenSetup.AcceptTATTest (0 ms)
[ RUN ] RouteGenSetup.BlockLinksTest
[error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1,pattern: TR
[error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1,pattern: TR