Find the answer to your Linux question:
Results 1 to 7 of 7
I have a problem - I have files with rows of data and I need to check if the next row (of the same type) has the NEXT date in ...
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    4

    Working on Dates in files

    I have a problem - I have files with rows of data and I need to check if the next row (of the same type) has the NEXT date in it so I need to extract a date in YYYYMMDD format from a row (easy enough) then add one day to it and compare it to the the next date I encounter on a subsequent row.

    So if I have
    DATE|20100530
    VAL|kahskjhd
    VAL|lajdlkjad
    DATE|20100601
    VAL|lah

    then I am happy

    If I have
    DATE|20100530
    VAL|kahskjhd
    VAL|lajdlkjad
    DATE|20100602
    VAL|lah

    Then I have a problem

    I can loop through the file simply enough and compare variables and output warnings but I am entirely stumpted by trying to add a day to a variable.

    Graham

  2. #2
    Linux User twoHats's Avatar
    Join Date
    Jan 2005
    Location
    NH, USA
    Posts
    276
    language is important here as most have some utilities to manipulate dates. The typical way is to convert to Julian date - add 1 - and convert back.

    Some librarys will have date arithmetic modules (or packages, etc) then it is easy. Almost all will have a way to convert to Julian and back.
    - Clouds don't crash - Bertrand Meyer

    registered Linux user 393557

    finally - hw to brag about - but next year it will look pitifully quaint:
    Athlon64 X2 3800 - 1G PC3200 - 250G SATA - ati radeon x300
    circa 2006

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    4
    Ideally I would like to do it in Shell script or Bash so I can do it onsite with my laptop which is an XP tablet running Cygwin - that way I don't need to get back to my office to use my server.

    I have seen lots of examples manipulating the system date or file date but nothing using a date from inside a file as the starting point.

    Graham

  4. #4
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    man date

    Code:
    date "--date=tomorrow 20100530" '+%Y%m%d'
    will output 20100531.

    Store that in a variable and check the next date matches that. If it does use that as the starting point for the following date.

    You should be able to implement this in bash:

    Code:
    previous_date=""
    while not_eof
    {
       current_line=read line
       if current_line is date line
       {
          current_date=extract_date()
          if previous_date is empty
          {
            if previous_date == current_date
            {
              OK
            }
            else
            {
              Fail
            }
          }
          
          previous_date=nextday(current_date)
       }
    }
    In a world without walls and fences, who needs Windows and Gates?

  5. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    4
    The code suggested above to produce the date plus 1 from the file value works:

    date "--date=tomorrow $LASTDATE" +%Y%m%d

    but when I try to put it into a variable it just displays the value on screen and when I later try to use it the variable is null. I know I am being feeble here and the answer is obvious but I am at a loss.

    I have tried all sorts of things but I need something like

    NEXTDATE= date "--date=tomorrow $LASTDATE" +%Y%m%d

    so I can test it later with

    if [ "${LINE:4:8}" != "NEXTDATE" ] ; then

    but I have obviously failed to grasp the syntax of the assignment - all help appreciated

    Graham

  6. #6
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    NEXTDATE= $(date "--date=tomorrow $LASTDATE" +%Y%m%d)
    echo $NEXTDATE
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  7. #7
    Just Joined!
    Join Date
    Aug 2010
    Posts
    4
    That worked beautifully (I had to take the space out after the = sign - just for any other people like me who struggle with the syntax):

    NEXTDATE=$(date "--date=tomorrow $LASTDATE" +%Y%m%d)

    Thanks blinky and nmset for your code expertise and twoHats for making me say what I actually needed.

    That has saved me looking through another 80,000 lines of data for that defect by eye (I had already looked through 30,000 lines and found two occurances). It found a few more.

    Mind you I was being paid by the hour so perhaps this wasn't such a good idea

    Graham

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...