Remove Lines that Match from File
Solution 1
some python: warning, I'm pretty new to it.
import io
import datetime
buffer = io.StringIO()
ago = datetime.date.today() - datetime.timedelta(days=30)
filename = "file"
with open(filename,"r") as f:
line = f.readline()
while line:
if line.startswith("#"):
date = (line.split())[1]
if date >= ago.isoformat():
buffer.write(line)
line = f.readline()
buffer.write(line)
line = f.readline()
with open(filename, "w") as f:
f.write(buffer.getvalue())
Solution 2
requires GNU date, and sponge
from the moreutils package to write back to the same file
awk -v ago="$(date -d '30 days ago' '+%F %T')" '
$1 == "#" && $2" "$3 < ago {getline; next}
{print}
' file | sponge file
Related videos on Youtube
Eric
Updated on September 18, 2022Comments
-
Eric over 1 year
I have a file that contains various dates and IPs that I need to do a daily purge of. The file format is:
# 2018-02-21 11:31:37 - user1 - This is a test. 1.1.1.1 # 2018-02-21 11:32:30 - user1 - This is also a test. 2.2.2.2 # 2018-03-06 21:12:44 - user2 - Another comment. 3.3.3.3
On a daily basis I want to remove any comment line + the IP below it that is older than 30 days. For that, I can easily do a bash variable of "LAST_MONTH=
date --date="-30 days" +%Y-%m-%d
" and I can get the lines easily enough by doing "DELETE=$(cat /var/www/html/ips | grep $DATE -A1)".From here, I could technically use sed to wipe out the lines such as "sed -i "s/$DELETE/test/g" "$FILE"" but there are hash tags and multiple lines involved so it's not really working for me right now.
What would you recommend as the best way to do this? I don't want to create another temp file if at all possible. I'd also be interested in both bash and Python solutions.
Thanks.