Difference between two date values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Difference between two date values
# 8  
Old 10-14-2015
Hello sam@sam,

On a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk.

Thanks,
R. Singh
# 9  
Old 10-18-2015
Hello Ravinder singh, As suggested I have tried using /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk but only nawk seems to generate results but getting negative results: below are the 3 sample outputs

Code:
bash-3.00$ /usr/xpg4/bin/awk -vstart=$START -vend=$END 'BEGIN{split(start, A,":");split(end, B,":");sec_start=A[1] * 60 + A[2];sec_end=B[1] * 60 + B[2];diff=sec_end - sec_start;print "Minutes \t Seconds" ORS int(diff/60) "\t\t " diff%60}'
Invalid form for variable assignment: -vend=00:37


bash-3.00$ /usr/xpg6/bin/awk -vstart=$START -vend=$END 'BEGIN{split(start, A,":");split(end, B,":");sec_start=A[1] * 60 + A[2];sec_end=B[1] * 60 + B[2];diff=sec_end - sec_start;print "Minutes \t Seconds" ORS int(diff/60) "\t\t " diff%60}'
bash: /usr/xpg6/bin/awk: No such file or directory

 awk -vstart=$START -vend=$END 'BEGIN{split(start, A,":");split(end, B,":");sec_start=A[1] * 60 + A[2];sec_end=B[1] * 60 + B[2];diff=sec_end - sec_start;print "Minutes \t Seconds" ORS int(diff/60) "\t\t " diff%60}'
Minutes          Seconds
0                0

expected output should be some thing like:
Code:
Minutes          Seconds
0                    18


bash-3.00$ START=`date +"%T" | awk -F":" '{print $2 FS $3}'`
bash-3.00$ echo $START
00:19
Code:
bash-3.00$  END=`date +"%T" | awk -F":" '{print $2 FS $3}'`
bash-3.00$  echo $END
00:37


Last edited by Don Cragun; 10-22-2015 at 03:20 AM.. Reason: Put sample output in bold CODE tags.
# 10  
Old 10-19-2015
You got multiple solutions for date manipulation using various shells (bash,ksh) on this specific topic.
Also the forum search engine provides in-depth date manipulation from various members.

The awk solution provided by Ravinder provided will be the toughest to maintain if you don't know awk.

Resort to shell only, since you are using bash, RudiC provided a bashism, also i provided a solution in my post using ksh shell.

Hope that clears things out
Regards
Peasant.
# 11  
Old 10-22-2015
Quote:
Originally Posted by sam@sam
Hello Ravinder singh, As suggested I have tried using /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk but only nawk seems to generate results but getting negative results: below are the 3 sample outputs

Code:
bash-3.00$ /usr/xpg4/bin/awk -vstart=$START -vend=$END 'BEGIN{split(start, A,":");split(end, B,":");sec_start=A[1] * 60 + A[2];sec_end=B[1] * 60 + B[2];diff=sec_end - sec_start;print "Minutes \t Seconds" ORS int(diff/60) "\t\t " diff%60}'
[B]Invalid form for variable assignment: -vend=00:37

... ... ...

In many versions of awk (including /usr/xpg4/bin/awk), you have to separate the -v option from its option-argument. Try:
Code:
bash-3.00$ /usr/xpg4/bin/awk -v start=$START -v end=$END 'BEGIN{split(start, A,":");split(end, B,":");sec_start=A[1] * 60 + A[2];sec_end=B[1] * 60 + B[2];diff=sec_end - sec_start;print "Minutes \t Seconds" ORS int(diff/60) "\t\t " diff%60}'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Changing CSV files with date . Subtracting date by values

Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies

2. AIX

Time Difference between date and date -u

Hi Everyone, We are having an issue with date and date -u in our AIX Systems. We have checked environment variable TZ and /etc/environment and however, we could not rectify the difference. >date Thu Mar 19 22:31:40 IST 2015 >date -u Thu Mar 19 17:01:44 GMT 2015 Any clue... (5 Replies)
Discussion started by: madhav.kunapa
5 Replies

3. Shell Programming and Scripting

Date difference

HI All , i need a bash script to find the number of days between two dates . Format YYYY-MM-DD THanks, Neil (1 Reply)
Discussion started by: nevil
1 Replies

4. Programming

Date difference

I tried the below code to find difference between two dates. It works fine if the day of the month is 2-digit number. But it fails when we have a single-digit day of month(ex:1-9). my code is as below. please help me soon. #!/usr/bin/perl -w use strict; use Time::Local; ... (2 Replies)
Discussion started by: anandrec
2 Replies

5. Homework & Coursework Questions

help with the date difference

1. The problem statement, all variables and given/known data: The problem i have is that i probably make a few mistake here in the code but don't know what it is and i try to get the date difference but don't know where to add the days_in_month function 2. Relevant commands, code,... (1 Reply)
Discussion started by: mgyeah
1 Replies

6. Shell Programming and Scripting

Difference in date

Dear all, I fancy that I'm pretty competent in ksh, but I have someone on HP-UX wanting me to script up a simple interface to handle user alterations rather than giving them high privileges to run up SAM. This is all fairly straightforward, but I'm stuck on an epoch date issue. When we have... (6 Replies)
Discussion started by: rbatte1
6 Replies

7. Shell Programming and Scripting

Calculate difference between two successive values

Hi, I have a file containing timestamps (at micro-seconds granularity). It looks like the following: 06:49:42.383818 06:49:42.390190 06:49:42.392308 06:49:42.392712 06:49:42.393437 06:49:42.393960 06:49:42.402115 Now I need a sed/awk script to take the difference of two successive... (2 Replies)
Discussion started by: sajal.bhatia
2 Replies

8. Shell Programming and Scripting

Korn Shell Variable values difference

I am using two shell scripts a.ksh and b.ksh a.ksh 1. Sets the value +++++++++++++++++ export USER1=abcd1 export PASSWORD=xyz +++++++++++++++++ b.ksh 2. Second scripts calls sctipt a.ksh and uses the values set in a.ksh and pass to an executable demo... (2 Replies)
Discussion started by: kunalseth
2 Replies

9. Shell Programming and Scripting

Bash script getting difference values from files

Hi I want to write a BASH script. I have files updated every hour from two platforms.The file names are : - Falcon_smsCounters_1200020708.log - Canari_smsCounters_1200020708.log 1200 refers to hour twelve and 020708 to 02 july 2008. Inside every file i have a list of parameters... (2 Replies)
Discussion started by: salimayoub
2 Replies

10. Linux

date difference

hi, i have 2 dates in the form: '20080315120030' and '20080310140030'. i.e. YYYYMMDDHHMMSS. i need a way of getting the difference between them using shell script. any thoughts? (14 Replies)
Discussion started by: muay_tb
14 Replies
Login or Register to Ask a Question