Bash script compare two date variables [duplicate] - bash

This question already has answers here:
How can I calculate time elapsed in a Bash script?
(20 answers)
Closed 5 years ago.
I'm trying to compare a date given by a user to a date in a file, basically a text file with lots of dates and times listed.
for example the user would enter a date such as 22/08/2007 and a time of 1:00, what i need the script to do is count how many dates in the text file are after the date given by the user.
I’ve managed to accomplish this by converting each date in the text file to unix timestamp and then comparing the two. Is there no way of simply comparing two dates in bash?
Thanks in advance

The GNU date command can convert a date into the number of seconds since 1970. Try this script:
#! /bin/bash
DATE=$(date -d "$3-$2-$1 01" '+%s')
COUNT=0
tr '/' ' ' | {
while read D M Y ; do
THIS=$(date -d "$Y-$M-$D 01" '+%s')
if (( THIS > DATE )) ; then
COUNT=$((COUNT + 1))
fi
done
echo $COUNT
}
It expects three arguments and the raw dates in stdin:
for D in $(seq 19 25) ; do echo $D/08/2007 ; done | ./count.sh 22 08 2007
3
It will work till 2038. ;-)

If you don't mind an external helper tool look at my dateutils. Your use case is covered by
dgrep -i '%d/%m/%Y %H:%M' '>=2007-08-22 01:00:00' < FILE | wc -l
where FILE is your file with the dates, and -i specifies the date format used in the file (I assumed dates like 22/08/2007 01:00 here). Matching lines will be printed, hence counting them gives you the information you were after.

...why don't you simply cut out the single numbers, rearrange them from the most signifcant to the less significant, put them toghether to form a new big number and then compare the other one? :) Suppose you have a date in both $1 and $2 and suppose the date format is dd-mm-yyyy (adding hours and minutes is trivial):
d1=`echo "$1" | cut -d "-" -f 1`
m1=`echo "$1" | cut -d "-" -f 2`
y1=`echo "$1" | cut -d "-" -f 3`
date1="$y1$m1$d1"
d2=`echo "$2" | cut -d "-" -f 1`
m2=`echo "$2" | cut -d "-" -f 2`
y2=`echo "$2" | cut -d "-" -f 3`
date2="$y2$m2$d2"
if [ "$date1" -gt "$date2" ]; then
#date1 > date2
else
#date2 >= date1
fi
Note that you need zeros for 1-digit fields, for example, dates like this will work:
01-01-2013
and dates like this will NOT
1-1-2013
Cheers :-)

The problem is that dates are printed in such a way that, string-wise, "1/1/2050 1:00" < "2/1/1999 0:00". And since there's no way for a script to know that something is a datetime without you saying so, you essentially have to convert any date to something that can be compared - Either you have to order the elements so that the most important (year) are first, etc. (like ISO dates) or you convert to a number.

the above command compares the date in form of integer and would work fine until you are comparing the dates of same year.
better idea is to break the dates into 3 parts of dd, mm and yyyy and then do a comparison. just as below:
sysdate=`date +%d%m%Y`
sys_dd=`echo $sysdate|cut -c1,2`
sys_mm=`echo $sysdate|cut -c3,4`
sys_yyyy=`echo $sysdate|cut -c5-8`
cd $dir_source #moving in directory where report are placed
for i in *.* #reading all the files present in directory and comparing with current sysdate
do
filename=$i
filedate=`echo $filename| cut -d '_' -f1`
file_dd=`echo $filedate|cut -c1,2`
file_mm=`echo $filedate|cut -c3,4`
file_yyyy=`echo $filedate|cut -c5-8`
if [ $sys_yyyy -lt $file_yyyy ]
then
echo "future cob file, check for the error"elif [ $sys_yyyy -gt $file_yyyy ]
then
echo "prev cob file , to be removed"
else
if [ $sys_mm -lt $file_mm ]
then
echo "future cob file, check for the error"
elif [ $sys_mm -gt $file_mm ]
then
echo "prev cob file , to be removed"
else
if [ $sys_dd -lt $file_dd ]
then
echo "future cob file, check for the error"
elif [ $sys_dd -gt $file_dd ]
then
echo "prev cob file , to be removed"
else
echo "file date is same is cob date, retaining the file as it is"
fi
fi
fi

Related

Compare a date in a bash script

I am doing a script to evaluate the last connection of a user, for this, I get the last time it was connected, and I extract the user + the date of the last connection, what I need to do now is to see if that date is greater or less than "2022-05-20" for example, but my problem is, that I do not know how to compare two dates in bash.
This is my code;
while [ $i -le $size_students ]
do
# Get the user's login
login=$(sed -n "${i}p" xxxx.txt)
# Get the user's data
user_data=$(curl -X GET -H "Authorization: Bearer ${bearer_token}" "https://xxxxxxxxx/${login}/locations_stats" --globoff)
# Get the user's last location
last_lotaction=$(echo $user_data | jq -r '.' | cut -d "\"" -f 2 | grep -Eo '[0-9]{4}-[0-9]{2}-[0-9]{2}' | head -n 1)
# if last_location is null or less than 2022-05-01, the user is not connected
echo `$login: $last_location`
The output is:
EnzoZidane: 2022-03-17
With your date formats, a simple string comparison should yield the desired result:
#!/bin/bash
[[ 2022-05-20 > 2022-05-19 ]] && echo yes
[[ 2022-05-20 < 2022-05-19 ]] || echo no
If you can guarantee that all your months and days are formatted with a leading zero where applicable, and the order is year-month-day, then you can probably just use a string comparison:
if [[ "2022-05-31" < "2022-06-01" ]]
then
echo true
fi
Convert all you dates to epoch first, then you can compare two integers:
if [[ $(date --date="$last_location" +%s) < $(date --date="2022-05-20" +%s) ]]; then
echo "before"
else
echo "after"
fi
Epoch time is always an integer number.

Bash script to compare a particular line in two different files, and compare dates [duplicate]

This question already has answers here:
Date comparison in Bash [duplicate]
(2 answers)
Closed 1 year ago.
I would like to compare line number 13 in two different files. That line contains date in the format: "Nov 8 00:46:57 2021 GMT"
I want to compare these two dates, and check which is less or greater than the other. Can someone please help me with this.
# Reads line 13 of file1.txt into date1
date1=$(awk 'NR==13' file1.txt)
# Reads line 13 of file2.txt into date2
date2=$(awk 'NR==13' file2.txt)
# converts date to seconds since the Epoch in Linux
date1=$(date -d "$date1" +%s)
date2=$(date -d "$date2" +%s)
if (( date1 >= date2 )); then
echo "Date in file1.txt is greater or equal"
else
echo "Date in file2.txt is greater"
fi
13a and 13b are your file names:
iso8601-13() {
date -d "`head -13 "$1" | tail -n 1`" --iso-8601=seconds
}
if [[ `iso8601-13 13a` < `iso8601-13 13b` ]] ; then
echo 13a is earlier
fi

Bash on macOS - Get a list of dates for every Saturday on a given year

In bash on macOS, I would like to write a small script with dates (or any other program that would do) that gives me a list of dates in the format yyyymmdd of every Saturday of a given year and saves it to a variable.
For example, if I wanted to have a list of dates for all Saturdays of the year 1850, it should somehow look like this:
var = [ 18500105, 18500112, 18500119, …, 18501228 ]
with the below code:
list=()
for month in `seq -w 1 12`; do
for day in `seq -w 1 31`; do
list=( $(gdate -d "1850$month$day" '+%A %Y%m%d' | grep 'Saturday' | egrep -o '[[:digit:]]{4}[[:digit:]]{2}[[:digit:]]{2}' | tee /dev/tty) )
done
done
However, the above command does not write anything in the array list although it gives me the right output with tee.
How can I fix these issues?
Modifying Dennis Williamson's answer slightly to suit your requirement and to add the results into the array. Works on the GNU date and not on FreeBSD's version.
#!/usr/bin/env bash
y=1850
for d in {0..6}
do
# Identify the first day of the year that is a Saturday and break out of
# the loop
if (( $(date -d "$y-1-1 + $d day" '+%u') == 6))
then
break
fi
done
array=()
# Loop until the last day of the year, increment 7 days at a
# time and append the results to the array
for ((w = d; w <= $(date -d "$y-12-31" '+%j'); w += 7))
do
array+=( $(date -d "$y-1-1 + $w day" '+%Y%m%d') )
done
Now you can just print the results as
printf '%s\n' "${array[#]}"
To set up the GNU date on MacOS you need to do brew install coreutils and access the command as gdate to distinguish it from the native version provided.
Argh, just realised you need it for MacOS date.
I will leave the answer for others that do not have that restriction, but it will not work for you.
This is not quite what you want, but close:
year=1850
firstsat=$(date -d $year-01-01-$(date -d $year-01-01 +%w)days+6days +%Y%m%d)
parset a 'date -d '$firstsat'+{=$_*=7=}days +%Y%m%d' ::: {0..52}
echo ${a[#]}
It has the bug, that it finds the next 53 Saturdays, and the last of those may not be in current year.
parset is part of GNU Parallel.
I didn't do much error checking, but here's another implementation.
Takes day of week and target year as arguments.
Gets the julian day of the first matching weekday requested -
gets the epoch seconds of noon on that day -
as long as the year matches what was requested, adds that date to the array and adds a week's worth of seconds to the tracking variable.
Lather, rinse, repeat until no longer in that year.
$: typeset -f alldays
alldays () { local dow=$1 year=$2 julian=1;
until [[ "$dow" == "$( date +%a -d $year-01-0$julian )" ]]; do (( julian++ )); done;
es=$( date +%s -d "12pm $year-01-0$julian" );
allhits=( $( while [[ $year == "$( date +%Y -d #$es )" ]]; do date +%Y%m%d -d #$es; (( es+=604800 )); done; ) )
}
$: time alldays Sat 1850
real 0m9.931s
user 0m1.025s
sys 0m6.695s
$: printf "%s\n" "${allhits[#]}"
18500105
18500112
18500119
18500126
18500202
18500209
18500216
18500223
18500302
18500309
18500316
18500323
18500330
18500406
18500413
18500420
18500427
18500504
18500511
18500518
18500525
18500601
18500608
18500615
18500622
18500629
18500706
18500713
18500720
18500727
18500803
18500810
18500817
18500824
18500831
18500907
18500914
18500921
18500928
18501005
18501012
18501019
18501026
18501102
18501109
18501116
18501123
18501130
18501207
18501214
18501221
18501228

How to Subtract a Date value in a variable

I have a problem to create a script. My problem is when trying to subtract a date. In my script I pass a date with the format dd/mm/yyyy as parameter and I need to compare with files in a directory that has the date in the filename. The date can be the actul or until 5 days before the day passed as parameter. My question is, how can I create an if condition for example that will compare the date as parameter and the last 5 days before it with the date that is specified in the filename ? I already searched in every place how can I do it and nothing could help me.
Thanks for now.
Jairo.
Not sure of your requirement, but this may help you(not the best of solutions)
assuming the file names are in format: file_11092013 , file_01092013
#!/bin/bash
PASSED_DATE=$1
date_diff() {
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d2 - d1) / 86400 ))
}
for file in `ls /home/mvaidya/test`
do
curr_file=`echo $file | awk 'BEGIN {FS="_"} {print $NF}'`
day1=`echo $curr_file| cut -c1-2`
month1=`echo $curr_file| cut -c3-4`
year1=`echo $curr_file| cut -c5-8`
day2=`echo $PASSED_DATE| cut -c1-2`
month2=`echo $PASSED_DATE| cut -c4-5`
year2=`echo $PASSED_DATE| cut -c7-10`
no_of_days=$( date_diff "$month2/$day2/$year2" "$month1/$day1/$year1" )
if [ $no_of_days -ge 5 ] ; then
echo "File "$file" more than 5 days old."
else
echo "File "$file" less than 5 days old."
fi
done
Eg:
$./exp1.sh 01/09/2013
File file_11092013 more than 5 days old.
$./exp1.sh 10/09/2013
File file_11092013 less than 5 days old.

Bash: Check current day time against "allow string"

This might be a bit strange. I have a string that indicates all of the "allowed" times per week. The String is 168 characters long. Each of the characters is either a 1 or a 0. Each character represents one hour of the week. So, the first 24 characters in the string indicate the "allowed" hours for Sunday. The next 24 characters are Monday and so on. The number 1 represents "allowed" and the number 0 represents not allowed. Here is a sample string:
000000111111111111111000000000111111111111111000000000111111111111111000000000111111111111111000000000111111111111111000000000111111111111111000000000111111111111111000
This string indicates allowed times between 6AM and 9PM Sunday through Saturday. Many times there will be different allowed hours for different days of the week. I'm trying to figure out it bash if the current day time is "allowed".
I've figured out that these tell me the current day and time:
DATE=$(date)
day=$(echo $DATE | cut -d' ' -f1)
time=$(echo $DATE | cut -d' ' -f4 | cut -d: -f1-2 | tr -d ':')
However, I can't figure out an elegant way of comparing those to my long string.
Any help would be greatly appreciated!
Please note: I am using BusyBox 1.19.4. That means that awk, sed, grep, etc may not be full-featured. :-(
EV
Here is another bash version which may help you. I am assuming that the input string is stored in a file and will be passed as an argument to the script :
i=`expr $(date +%w) \\* 24 + $(date +%H) + 1`
i=$(cat $1 | awk -v I=${i} '{print substr($0,I,1)}')
if [ $i -eq 1 ]
then echo "BUSY"
elif [ $i -eq 0 ]
then echo "FREE"
else
echo "error : input string is shorter than expected"
fi
If you want to pass the string as a parameter instead of a file, just replace the 'cat $1' part of the 2nd statement to :
echo $1
NB : In case input string is shorter than the i value, you will two error messages from shell "[: -eq: unary operator expected" for the two if conditions along with the 3rd echo statement. So you may want to improve the if conditions a bit.
Again, the above piece of code is valid only when the start of the week is Sunday 0th hour. Otherwise some other mapping must be used.
Interesting question!
Here is awk solution to do it in one shot. (without date stuff):
awk -v FIELDWIDTHS="24 24 24 24 24 24 24" '{split(strftime("%w,%H", systime()),t,",");for(i=0;i<7;i++)a[i]=$(i+1);print substr(a[t[1]],t[2]+1,1)?"OK":"NOK"}' file
for better reading:
awk -v FIELDWIDTHS="24 24 24 24 24 24 24" '{
split(strftime("%w,%H", systime()),t,",")
for(i=0;i<7;i++) a[i]=$(i+1)
print substr(a[t[1]],t[2]+1,1)?"OK":"NOK"
}' file
if I run this now, (here is Wed. 10:58 AM), it outputs:
OK
please test it with your real data, and report if it works. :)
Here is a bash only version
startoweek=`date +%s -d 20130717`
#1373999400
current=$(date +%s)
let diff=(${current}-${startoweek})/3600
if [ $diff -ge 168 ]
then echo "Old Timetable"
exit
fi
work=000000111111111111111000000000111111111111111000000000111111111111111000000000111111111111111000000000111111111111111000000000111111111111111000000000111111111111111000
allowed=${work:$diff:1}
if [ $allowed -eq 1 ]
then echo "Welcome"
elif [ $allowed -eq 0 ]
then echo "Go Home"
else
echo "Error"
fi

Resources