LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 09-18-2011, 01:21 PM   #1
csegau
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, 03:15 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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:
Code:
#! /bin/bash
#
#   LQcsegau.sh
#      run as: LQcsegau.sh 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;
    fi; 
#
#  Read from data file
#
done < LQcsegau.dat
--- rod.

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


Reply


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 01:49 AM
[SOLVED] How to subtract two dates in a file to get number of days? btacuso Linux - Newbie 2 07-13-2010 12:33 PM
Need help writing a renaming script (DDMMYYYY to YYYYMMDD format) Zombie13 Programming 5 01-15-2010 07:56 PM
Dates to epoch format sebelk Programming 3 10-26-2009 03:50 PM
find YYYYMMDD and YYYYMMDD difference kpachopoulos Programming 1 09-22-2007 11:37 AM


All times are GMT -5. The time now is 08:14 AM.

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