Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed delete lines from a logfile that respect numbers in another file

Tags:

bash

I have a logfile that is starting to grow in size, and I need to remove certain lines that match a given pattern from it. I used grep -nr for extracting the target lines and copied them in a temp file, but I can't figure how can I tell sed to delete those lines from the log file. I have found something similar here: Delete line from text file with line numbers from another file but this doesn't actually delete the lines, it only prints the wanted output.

Can anyone give me a hint?

Thank you!

like image 639
Alex Avatar asked Feb 01 '26 15:02

Alex


1 Answers

I think, what you really need is sed -i '/pattern/d' filename.

But to answer your question:
How to delete lines matching the line numbers from another file:
(Assuming that there are no special characters in the line_numbers file, just numbers one per line...)

awk 'NR==FNR{a[$0]=1; next}; !(FNR in a)' line_numbers input.log
like image 69
anishsane Avatar answered Feb 03 '26 06:02

anishsane