Hi,
I have a Unix mail file that I need to truncate, based on the date of the messages. For those not familiar with the format, it is a single file for each user, with the first line of the mail message looking like the following:
Each succeeding message is separated by a blank line. I need to only keep 2 days worth of messages. What I would like to do is find the line for the first message from 2 days ago, and then delete all the lines in the mail file above this entry. Any ideas?
By not displaying a sample of the input and output data, you will not allow people to understand your requirement properly, thus they will not answer it.
#!/bin/bash
mail=/var/spool/mail/$USER
#get the first date that's within last two days:
D=$( egrep "^From " $mail | #filter the lines starting with 'From '
sed 's/^From [^ ][^ ]*//' | #get only the date
while read D ; do #loop through dates
[[ $((`date +%s`-`date -d "$D" +%s`)) -lt $((2*24*3600)) ]] && echo $D && break #compare and exit loop as soon as you find one that's within 2 days
done )
#print only from this date on:
sed -n "/^From .* $D/,$ p" $mail