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.
# 2018-02-21 11:32:30 - user1 - This is also a test.
# 2018-03-06 21:12:44 - user2 - Another comment.

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.



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():
                line = f.readline()
        line = f.readline()

with open(filename, "w") as f:

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}
' file | sponge file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.