Issue
I have a file that looks like:
[----------] 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, rounds: LLLLLL, pattern: TR
[2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1, rounds: LLLLLL, 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, rounds: LLLLLL, pattern: TR
[2021-03-08 22:55:53.938] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1, rounds: LLLLLL, pattern: TR
I want to get rid of the date and time stamp (e.g., [2021-03-08 22:55:53.937]
). To do so, I tried using the following sed
command:
sed -i '/^\[2021-/s/^.*\]\ //1' output.txt
However, the [info]
or [error]
bracket to the right of the time stamp also gets deleted:
[----------] 7 tests from RouteGenSetup
[ RUN ] RouteGenSetup.AcceptTATTest
TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1, rounds: LLLLLL, pattern: TR
TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1, rounds: LLLLLL, pattern: TR
TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1, rounds: LLLLLL, pattern: TR
[ OK ] RouteGenSetup.AcceptTATTest (0 ms)
[ RUN ] RouteGenSetup.BlockLinksTest
TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1, rounds: LLLLLL, pattern: TR
TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1, rounds: LLLLLL, pattern: TR
When I try
sed -i '/^\[2021/s/[^\]]*//' output.txt
the first open bracket deletes instead of the first closed bracket:
[----------] 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, rounds: LLLLLL, pattern: TR
2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1, rounds: LLLLLL, 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, rounds: LLLLLL, pattern: TR
2021-03-08 22:55:53.938] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1, rounds: LLLLLL, pattern: TR
And trying
sed -i '/^\[2021/s/^[^\]]*\]//' output.txt
produces no change. I'm not sure how to fix this. I tried a number of things recommended in similar posts but no luck.
NOTE: This is not a duplicate of sed - regex square brackets detection in Linux because here, the problem is different: only match a substring between the start of string and the first ]
+ space if the line starts with a certain prefix.
sed replace a word at a line which begins with a specific pattern using does not explain how to get the lines that start with a special character and then match the string from start till the first ]
+ space.
Solution
You can use
sed -i '/^\[2021-/s/^[^]]*] //' file
Here,
/^\[2021-/
- finds lines that start with[2021-
s/^[^]]*] //
- matches any zero or more chars other than]
from the start of the string till the first]
and then a space and removes these matches (since the RHS is empty).
Note [^]]
is a negated bracket expression that matches any char but ]
. Since bracket expressions do not support regex escape sequences, ]
needs to be the first char in the bracket expression, you cannot use \]
anwhere in a bracket expression.
See an online demo:
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, rounds: LLLLLL, pattern: TR
[2021-03-08 22:55:53.937] [info] TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1, rounds: LLLLLL, 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, rounds: LLLLLL, pattern: TR
[2021-03-08 22:55:53.938] [error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1, rounds: LLLLLL, pattern: TR'
sed '/^\[2021-/s/^[^]]*] //' <<< "$s"
Output:
[----------] 7 tests from RouteGenSetup
[ RUN ] RouteGenSetup.AcceptTATTest
[info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1, rounds: LLLLLL, pattern: TR
[info] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1, rounds: LLLLLL, pattern: TR
[info] TimeSlotAssignmentTable::LogTATRow(Add): 6.1 -> 7.1, rounds: LLLLLL, pattern: TR
[ OK ] RouteGenSetup.AcceptTATTest (0 ms)
[ RUN ] RouteGenSetup.BlockLinksTest
[error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 2.1, rounds: LLLLLL, pattern: TR
[error] TimeSlotAssignmentTable::LogTATRow(Add): 1.1 -> 5.1, rounds: LLLLLL, pattern: TR
Answered By - Wiktor Stribiżew