Remove multiple occurrences of similar lines except the last
asked 15 hours ago by @qa-fglvedu4lb6xjslxlbxy 0 rep · 234 views
I have a file where a pattern repeats a various number of times between lines that are relevant. I want to keep only the last of the lines that repeat in each occurrence of the repetition. This is the file:
important information 1
important information 2
2026-07-10T11:12:31.013620-04:00 <-- delete
2026-07-10T11:13:34.058824-04:00 <-- delete
2026-07-10T11:14:37.116946-04:00 <-- delete
2026-07-10T11:15:09.647148-04:00 <-- keep
important information 3
important information 4
important information 5
2026-07-10T11:15:12.662378-04:00 <-- keep
important information 6
important information 7
2026-07-10T11:15:18.232970-04:00 <-- keep
important information 8
2026-07-10T11:15:40.152816-04:00 <-- delete
2026-07-10T11:16:43.190958-04:00 <-- delete
2026-07-10T11:17:46.237995-04:00 <-- delete
2026-07-10T11:18:49.282324-04:00 <-- delete
2026-07-10T11:19:52.327966-04:00 <-- delete
2026-07-10T11:28:16.700459-04:00 <-- delete
2026-07-10T11:29:19.748261-04:00 <-- delete
2026-07-10T11:30:22.788192-04:00 <-- keep
important information 9
important information 10
important information 11
what I have tried:
tac file.txt | awk '/2026-/ {if (f) next; f=1}1' | tac
gives me this:
important information 1
important information 2
important information 3
important information 4
important information 5
important information 6
important information 7
important information 8
2026-07-10T11:30:22.788192-04:00 <-- keep
important information 9
important information 10
important information 11
but I want this:
important information 1
important information 2
2026-07-10T11:15:09.647148-04:00 <-- keep
important information 3
important information 4
important information 5
2026-07-10T11:15:12.662378-04:00 <-- keep
important information 6
important information 7
2026-07-10T11:15:18.232970-04:00 <-- keep
important information 8
2026-07-10T11:30:22.788192-04:00 <-- keep
important information 9
important information 10
important information 11