Reputation: 721
I have a file named Test1.dat and its content's are as follows
Abcxxxxxxxxxxx_123.dat@10:10:15
Bcdxxxxxxxxxxx_145.dat@10:15:23
Cssxxxxxxxxxxx_567.dat@10:26:56
Fgsxxxxxxxxxxx_823.dat@10:46:56
Kssxxxxxxxxxxx_999.dat@11:15:23
Please note that after the @ symbol it is the HH:MM:SS format that follows. My question now is I want to calculate the time difference between the current time and the time present in the files and fetch only those filenames where time difference is more than 30 Mins. So if current time is 11:00:00, I want to fetch files that have arrived 30 minutes before so basically the first three files.
Upvotes: 2
Views: 87
Reputation: 11
Could be this the answer?
awk -F@ '{ts = systime(); thirty_mins = 30 * 60; thirty_mins_ago = ts - thirty_mins; if ($2 < strftime("%H:%M:%S", thirty_mins_ago)) print $2 }' <file.txt
strftime
is a GAWK (gnu awk) extention.
Upvotes: 1
Reputation: 41456
This awk
should do:
awk -F@ '$2>=from && $2<=to' from="$(date +%H:%M:%S -d -30min)" to="$(date +%H:%M:%S)" file
If you only need to get the last 30 min (In you case you need the first one since you do not like 11:30):
awk -F@ '$2>=from' from="$(date +%H:%M:%S -d -30min)" file
Upvotes: 2
Reputation: 5092
You can also use bash script and get the result
#!/bin/bash
current=`date '+%s'`
needed_time=`echo "$current - 60 * 30" | bc`
while read line ; do
time=`echo $line |sed -r 's/[^@]*.(.*)/\1/g'`
user_time=`date -d $time '+%s'`
if [ $user_time -le $needed_time ] ; then
echo "$line"
fi
done < file_name
Upvotes: 1