简体   繁体   中英

how do you through loop a certain date range

I have files in a directory that are date based but not obviously date-stamped.

File_yyyymmdd_record.log

These are lying around in a directory for a few years worth of time. Now if these were simply numbers all I needed to do was get the difference and incremenet a counter to push the value

var=substring( File_yyyymmdd_record.log ) /* get the yyyymmdd part */
var2=substring( File2_yyyymmdd_record.log ) /* get the yyyymmdd part */
delta=var2-var1
set i=delta and loop through to get the values for all these recordID's ( record ID    is the yyyymmdd part ) 

The problem is if I have 2 different months and also years in the directory say 20131210 and 20140110 the difference not going to gimme all the recordID's in that directory , since, when it spills over to the next month the plain numeric calculation is not applicable- it should be a date based calculation. what I want to do is use 2 input parameters to the shell

 shell.sh recordID1 recordID2

and based on these it will find all records and store them some place and loop through each record as an input like this

find <dir> -iname recordID* ...<some awk and sed here> |
   while read recordID ;
   do <stuff >
   done

Anyway this can be achieved esp in 2 contexts- First the date calculation part and the other is to store these recordID's so I can cycle through them. Maybe echo them to a tmp file is what comes off the bat.

For the date calculation part - I tried this and it works . But not sure if it will falter some time / situation

echo $((($(date -u -d 2010-04-29 +%s) - $(date -u -d 2010-03-28 +%s)) / 86400))

So given recordID1 as 20100328 I have 32 days recordID's to look for in that directory. You have to advance dates for 32 days from recordID1 and store them some place. How best can all this be done.

Something like this should do:

s=20130102 # start date
e=20130202 # end date

sep=$(date +"%s" -d"$s") # conv to epoch
eep=$(date +"%s" -d"$e")
for f in *.log; do
    d=$(date +"%s" -d$(sed -n 's/^[^_]*_\([^_]*\)_[^_]*.log/\1/p' <<< "$f"))
    if [ "$d" -ge "$sep" ] && [ "$d" -le "$eep" ]; then
        echo $f
    fi
done

I got your points, you need find out log files with file name between 20131210 and 20140110 . (no need convert to epoch time)

#! /usr/bin/bash

sep=20131210 
eep=20140110 

find /DIR -type f -name "*.log" |while read file
do
  d=${file:5:8}
  if [ "$d" -ge "$sep" ] && [ "$d" -le "$eep" ]; then
     do <stuff >
  fi
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM