View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-18-2011, 12:21 PM   #1
LQ Newbie
Registered: Oct 2009
Posts: 27

Rep: Reputation: 0
no of days between two dates in yyyymmdd format

Hi All,

I have a file which has 3 column, column 2nd and 3rd have date in yyyymmdd format.

i have to find find the no. of days between two dates.

ex: file
column1 column2 column3
A 20090909 20100908
A 20080102 20090107
B 20110909 20110910
A 20001010 20030405

so i have to find out no of days when "A" was present.
this input "A" is command line argument, it can be "B" too..

-Thanks in advance
Old 09-18-2011, 02:15 PM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
If I understand your question correctly, you want to calculate the difference between dates, in days, for records that match a specified argument. If so, this should be close:
#! /bin/bash
#      run as: A    # (use record type as desired)
#      -  uses embedded data file name; customize as necessary
while read rec; do 
    #  Split each record into separate fields
    rectype=$(echo $rec | cut -d' ' -f1);
    d1=$(echo $rec | cut -d' ' -f2);
    d2=$(echo $rec | cut -d' ' -f3);
    #   If record type matches that specified on commandline...
    if [ "$rectype" = "$1" ]; then 
        #   normalize dates to seconds-since-epoch
        date1=$(date -d "$d1" "+%s" ); 
        date2=$(date -d "$d2" "+%s" );
        #  Calculate difference in seconds, convert back to days, and report
        days=$(( ($date2-$date1)/86400 )); 
        echo $days;
#  Read from data file
done < LQcsegau.dat
--- rod.

Last edited by theNbomr; 09-18-2011 at 02:36 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Convert from DD/MM/YYYY to YYYYMMDD hattori.hanzo Programming 6 06-07-2011 12:49 AM
[SOLVED] How to subtract two dates in a file to get number of days? btacuso Linux - Newbie 2 07-13-2010 11:33 AM
Need help writing a renaming script (DDMMYYYY to YYYYMMDD format) Zombie13 Programming 5 01-15-2010 06:56 PM
Dates to epoch format sebelk Programming 3 10-26-2009 02:50 PM
find YYYYMMDD and YYYYMMDD difference kpachopoulos Programming 1 09-22-2007 10:37 AM

All times are GMT -5. The time now is 12:19 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration