Subtract two dates in different lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Subtract two dates in different lines
# 1  
Old 07-07-2005
Subtract two dates in different lines

Hi Friends Smilie
I have a long file having fields in the form :
Field1 yy/mm/dd hh:mm:ss Duration(Sec)
line 1) 123123 05/11/30 12:12:56 145
line 2) 145235 05/11/30 12:15:15 30
line 3) 145264 05/11/30 13:14:56 178
.
.
I want to subtract yy/dd/dd hh:mm:ss in line (2) from yy/mm/dd hh:mm:ss in line (1) and compare the result(in seconds) with the duration(sec) of line(1).
and echo a message whether it is < or >. I want to apply this to the long file This can be done easily in excel as we usually drag a formula in excel. But in unix how I don't know.
Please Dont refer me to Datecalc as I want specific script to do so.
Thanks In advance.
# 2  
Old 07-11-2005
Here is a starting point for you.

Have converted your line (123123 05/11/30 12:12:56 145) into time since epoch.

Code:
#! /bin/sh

while read line
do
DATE=$(echo $line | awk '{ print $2 }')
TIME=$(echo $line | awk '{ print $3 }')
echo $(date -d "$DATE $TIME" +%s)

done < vanand.txt

vanand.txt contains

123123 05/11/30 12:12:56 145
145235 05/11/30 12:15:15 30
145264 05/11/30 13:14:56 178
.
.

After this, I think you need to store the values in an array and then do the comparison.

Cheers,
vino
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - printing new lines based of 2 dates

I have some test data that is seperated out into annual records, each record has a start date (COL7), an end date (COL8) and a maturity date (COL18) - What I need to do is ensure that there is one record to cover each year right up until Maturity date (COL18). In the first group of the below... (10 Replies)
Discussion started by: Ads89
10 Replies

2. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. UNIX for Advanced & Expert Users

How to get the Missing dates between two dates in the table?

Hi Am Using Unix Ksh ... I have a Table called date select * from date ; Date 01/02/2013 06/02/2013 I need the output as Missing Date 01/02/2013 02/02/2013 03/02/2013 04/02/2013 05/02/2013 06/02/2013 (2 Replies)
Discussion started by: Venkatesh1
2 Replies

4. Shell Programming and Scripting

Compare & subtract lines in files by column using awk.

I have two files with similar column pattern as given below : 2 sample lines from file1 are given below. 18 12630 . G T 49.97 . AC=2;AF=1.00;AN=2;DP=3;Dels=0.00;FS=0.000;HRun=0;HaplotypeScore=0.0000;MQ=60.00;MQ0=0;NDA=1;QD=16.66;SB=-0.01 GT:AD:DP:GQ:PL ... (2 Replies)
Discussion started by: vaibhavvsk
2 Replies

5. Shell Programming and Scripting

Extract the lines between two dates

Dear experts I am new bee to scripting... Pl. help me getting the lines between two strings. I am getting the oracle sql output as below. and would like to get the time from system date compare with the date in sql output file and extract the lines. if system time is 2012-07-01 19:15:00 get... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

6. UNIX for Dummies Questions & Answers

How to write the dates between 2 dates into a file

Hi All, I am trying to print the dates that falls between 2 date variables into a file. Here is the example. $BUS_DATE =20120616 $SUB_DATE=20120613 Output to file abc.txt should be : 20120613,20120614,120120615,20120616 Can you pls help me accomplish this in LINUX. Thanks... (5 Replies)
Discussion started by: dsfreddie
5 Replies

7. Shell Programming and Scripting

lines between two dates in a file

Hi Every body I had a data in file as shown.. Now i want to get the data and insert into other file but between the two date ranges or from a past date to current date..Please Guide me in doing that.. Below is the data that looks like... child process 2588 still did not exit, sending a... (2 Replies)
Discussion started by: Reddy482
2 Replies

8. Programming

SQL: find if a set od dates falls in another set of dates

Don't know if it is important: Debian Linux / MySQL 5.1 I have a table: media_id int(8) group_id int(8) type_id int(8) expiration date start date cust_id int(8) num_runs int(8) preferred_time int(8) edit_date timestamp ON UPDATE CURRENT_TIMESTAMP id... (0 Replies)
Discussion started by: vertical98
0 Replies

9. Shell Programming and Scripting

How to subtract the adjacent lines from a single column?

Hi All, I have a file with only one column and i need to subtract the adjacent lines of the same column and print it in the same column. For Example: (Input) Col1 5 10 12 6 9 12 5 . . . .output should be like this: (12 Replies)
Discussion started by: Fredrick
12 Replies

10. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies
Login or Register to Ask a Question