Compare the system date with date from a text file
I get the date that's inside a text file and assigned it to a variable. When I grep the date from the file, I get this,
So I only crop out the date, with this command
The result would be
How do I convert this date to the number of seconds, so I can compare it to the system date? If it is over 2 days old.
I have this code but it doesn't work. I'm getting an error message when I ran it. I think its because $dateFile is a text file and it doesn't know how to convert it.
Any help would be appreciated.
Last edited by rbatte1; 02-21-2017 at 08:31 AM..
Reason: Changed bold text to CODE for file contents
(Edit: I'd initially typo'd the "2 days" as "2 months" in the output routine. Fixed now, sorry.)
I think I have a solution for you. The script is:
You'll notice that above the 'eval' lines (a command I'm using to do the arithmetic here) there are also commented-out lines using Bash's own built-in 'let' command, which can also do arithmetic. If you don't have 'expr' on your system, then you can comment out or remove the 'expr' lines and go with the 'let' lines instead. I've tested it with both, and (for this one single test input file, it must be noted) all was well.
Here is a transcript of a sample session, using the exact test date string you provided as the input in 'date.txt'.
Jul 28 14:57 2017 GMT
How do I convert this date to the number of seconds, so I can compare it to the system date? If it is over 2 days old.
I have this code but it doesn't work. I'm getting an error message when I ran it. I think its because $dateFile is a text file and it doesn't know how to convert it.
Any help would be appreciated.
Edit: I just noticed you have cut the minutes value out of the original date - I'm assuming this was a mistake and your cut command should probably have been cut -d ':' -f 2-4
If your system has GNU date you can use date -d STRING to display time described by STRING eg:
If you don't have GNU date you can use a perl program to achieve the same thing (most systems seem to have perl available).
save this a epoch.pl and chmod +x epoch.pl:
The you can call it like this
Last edited by Chubler_XL; 02-20-2017 at 04:45 PM..
Your solution is functional but can I make a few suggestions to help improve it.
1. Awk can open an read files without the help of cat
2. No need to change order of the fields GNU date would accept a date in the format anyway.
3. OP requires matching on line containing "After :"
OP is only wanting to match a filedate older than 2 days, the code above would also match filedate up to 2 days in the future.
OP was already using Arithmetic Expansion to calculate differences between epoch times.
I believe $((expression)) is superior to let as its more portable and is much better than using /usr/bin/expr as this wastes resources starting a new process and loading the expr command binary into memory to execute it.
I'd simply go with:
Last edited by Chubler_XL; 02-20-2017 at 05:14 PM..
These 2 Users Gave Thanks to Chubler_XL For This Post:
We want to call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below:
abc.txt
------------------
Code:
line1
line2
line3
$$EDWS_DATE_INSERT=08-27-2019
line4
$$EDWS_PREV_DATE_INSERT=08-26-2019
I am trying to write a... (3 Replies)
I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt .
I don't want to delete... (2 Replies)
Hi! I am trying to read a file and every line has a specific date as one of its fields.
I want to take that date and compare it to the date today plus 6 days.
while read line
do
date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD
if ; then
...proceed commands
... (1 Reply)
In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line.
I thought I would... (4 Replies)
Hello gurus,
I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4:
NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
I am currently running the following Korn shell script which works fine:
#!/usr/bin/ksh
count=`db2 -x "select count(*) from schema.tablename"`
echo "count"
I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
HI,
I have some files in my Linux machine that are very old and occupy a HUGe amount of space. I am trying to delete these files from the system so that it will be easy for me to add some files. I would like to know if this can done through a Perl or a shell script.
What i want to do is i... (6 Replies)
Hello everyone, I'm in desperate need of your help for this challenge I have below:
The run_dt_tbl has only 1 column and 1 row that contains a particular date in format mmddyy. I want to some how compare today's date with the date in this table that will be preset by someone else. If the dates... (2 Replies)
Hi I am very new to scripting,
Can someone show me how to (in unix shell script) compare the system's date with a date in a file. The requirement is to somehow open this file (which will only have a date in it) and compare it with today's date. If they are equal execute a procedure below but if... (4 Replies)