Remove lines matching pattern, plus any lines following it matching a different pattern
1
vote
3
answers
326
views
Let me preface I am not sure if this question has been asked before, I have been Googling for answers but came up short.
I want to use standard Linux/Unix commands (running this on FreeBSD) to exclude lines from a log file that match a pattern. The log file also includes "last message repeated x times" to condense log entries.
As an example, I want to take this:
May 27 2023 11:07 relevant information #1
May 27 2023 11:07 relevant information #2
May 27 2023 11:08 last message repeated 3 times
May 27 2023 11:08 useless information #1
May 27 2023 11:08 last message repeated 5 times
May 27 2023 11:09 last message repeated 8 times
May 27 2023 11:09 relevant information #3
May 27 2023 11:09 useless information #2
May 27 2023 11:10 useless information #3
May 27 2023 11:10 last message repeated 6 times
And get this output:
May 27 2023 11:07 relevant information #1
May 27 2023 11:07 relevant information #2
May 27 2023 11:08 last message repeated 3 times
May 27 2023 11:09 relevant information #3
I've gotten as far as using sed commands to do this, but I don't know enough about how it works to figure it out. I am especially lost when it comes to the log lines that have multiple "last message repeated" following it. Here's what I'm working with currently:
sed '/useless information/{d;N;/last message repeated/d;}' ./logfile.txt
The above first deletes matching lines containing "useless information", then adds the next line to the namespace with N
, and then is supposed to delete the resulting line if it contains "last message repeated". But it is only deleting the lines with "useless information".
Asked by ekrekeler
(13 rep)
May 27, 2023, 04:49 PM
Last activity: Jun 15, 2025, 04:20 AM
Last activity: Jun 15, 2025, 04:20 AM