Monday, March 14, 2011

how to recursively generate a date column

Today a dear friend of mine needed to add a column to some files she was analyzing getting them out of the mess of MS excel and inside the thin and nice gnuplot. 
The date column required a nice format : gg/mm/yy:hh.mm
So she asked me to spend a couple of min to help her in a dirty and quick way which follows:

  • First of all I thought to generate epoch of the starting date and last one so to deal with seconds and easily increment them of a fixed amounf (3600sec = 1hour)
START=`date -d "12/6/2005 16:00" +%s`
LAST=`date -d "12/31/2006 16:00" +%s`;echo $LAST
DELTA=3600
  • then generate the dates in epoch and convert in the desired format from cmd line 
for ((i=$START; i<=$LAST; i+=$DELTA)); do date -d @$i +%d/%m/%Y:%H.%M;done >dates.dat

  • check if the lenght is the same as data file with wc
  • Last to merge with the data file 
paste dates.dat data.dat >final.dat

hope this could be helpful 

alex 

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. bug (there are repeated entries in dates and one impossible to create)
    respectively use:
    $ uniq -d

    $ date -d "03/26/2006 2:00" +%s
    date: invalid date `03/26/2006 2:00'

    ReplyDelete
  3. just in case to add a delimiter use the following :
    in this case ";"
    nawk -v OFS=";" '$1=$1' filename

    ReplyDelete