0

I am using ksh, and have a text file with several lines of following content. I am trying to compare the date part to current date and find the difference in days and then output the "Server" and "Qmgr" part of the file to another file

Here is my input file

server1        :       Qmgr1      :       20 July 2017
server2        :       Qmgr2      :       22 July 2017
server3        :       Qmgr3      :       22 Dec 2015

if the difference between the current date and the date in the file is less than 90 days, then expected output should be

server3 : Qmgr3

What could be the easiest way to do this.

  • This question is about programming, and you don't even say what language you want to use. I'm assuming bash. – X3MBoy Nov 18 '15 at 16:32
  • re the date part, GNU date seems to understand "20 July 2017" just fine: /bin/date +%s -d '22 July 2017' gives 1500674400. – Ulrich Schwarz Nov 18 '15 at 16:39
  • If it's ksh, then it's probably AIX and that may well not have gnu tools. – Sobrique Nov 18 '15 at 16:48
  • It works using date command. how do I do this within a shell script to go through every line , convert to epoch, compare to current date and output the Qmgr part of the file if the difference is more then x days. – MO12 Nov 18 '15 at 16:54
  • You would need to provide a sample of your input file, and a sample of what should be provided. You also need to edit your question to include this new update. Or ask a new question and link it to this. – roaima Nov 18 '15 at 16:57
0

Try this. This script reads the files listed on the command line or standard input and prints the server and qmgr values for each line where the date is 90 days or less in the future.

#!/bin/sh

# also works with ksh, bash, zsh

now=$(date +%s)
ninety_days=$(( 90 * 24 * 60 * 60 ))

cat "$@" |
while read server colon qmgr colon2 qdate
do
    future=$(date +'%s' -d "$qdate")
    if [  $((future - now))  -lt  $ninety_days  ]
    then
        echo "$server $colon $qmgr"
    fi
done

Explanation:

Store the number of seconds between "the epoch" (1970-01-01 UTC) and the current time in the variable now:

now=$(date +%s)

Calculate the number of seconds contained in 90 days:

ninety_days=$(( 90 * 24 * 60 * 60 ))

Convert the date which was read from the last 3 fields of the record into the number of seconds between the "epoch" and that date. Store in the variable future (so-named for clarity when comparing to the variable now):

    future=$(date +'%s' -d "$qdate")

Subtract now from future to get the number of seconds between the current date and the date in the file. We've calculated the number of seconds contained in 90 days, so we can just compare that to the difference between future and now.

    if [  $((future - now))  -lt  $ninety_days  ]
  • Robert sorry for the late reply. This worked for me. Thank you.. Can you elaborate "FUTURE" and logic within if condition please – MO12 Jan 12 '16 at 21:49
  • Sure, please see the edited answer. – RobertL Jan 14 '16 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.