K
K
ksvdon2014-12-05 00:16:57
bash
ksvdon, 2014-12-05 00:16:57

How to explain this phenomenon (recalculation of time using bash)?

There is a list with dates. I need to recalculate dates (add some value in "Unix time" format). The list initially looks like this:

TS USER
.....
2014-12-02 22:00:00
2014-12-02 21:00:00
2014-12-02 21:00:00
2014-12-02 21:00:00
2014-12-02 20:00:00
2014-12-02 20:00:00
2014-12-02 20:00:00
2014-12-02 19:00:00
2014-12-02 19:00:00
2014-12-02 18:00:00
2014-12-02 18:00:00
2014-12-02 18:00:00
2014-12-02 18:00:00
2014-12-02 17:00:00
2014-12-02 17:00:00
2014-12-02 17:00:00
2014-12-02 17:00:00
2014-12-02 16:00:00
2014-12-02 16:00:00
2014-12-02 15:00:00
2014-12-02 15:00:00
2014-12-02 14:00:00
2014-12-02 14:00:00
2014-12-02 14:00:00
2014-12-02 14:00:00
2014-12-02 13:00:00
2014-12-02 12:00:00
2014-12-02 12:00:00
2014-12-02 12:00:00
2014-12-02 12:00:00
2014-12-02 11:00:00
2014-12-02 11:00:00
2014-12-02 02:00:00
2014-12-02 02:00:00
2014-12-02 02:00:00
2014-12-02 01:00:00
2014-12-02 01:00:00
2014-12-02 01:00:00
2014-12-02 00:00:00
2014-12-02 00:00:00
2014-12-02 00:00:00
2014-12-01 23:00:00
2014-12-01 23:00:00
2014-12-01 22:00:00
2014-12-01 22:00:00
2014-12-01 22:00:00
2014-12-01 22:00:00
2014-12-01 21:00:00
2014-12-01 21:00:00
2014-12-01 21:00:00
2014-12-01 20:00:00
2014-12-01 20:00:00
2014-12-01 20:00:00
2014-12-01 19:00:00
2014-12-01 19:00:00
2014-12-01 18:00:00
2014-12-01 18:00:00
2014-12-01 18:00:00
2014-12-01 18:00:00
2014-12-01 17:00:00
2014-12-01 17:00:00
2014-12-01 17:00:00
2014-12-01 16:00:00
2014-12-01 16:00:00
2014-12-01 16:00:00
2014-12-01 15:00:00
2014-12-01 15:00:00
2014-12-01 15:00:00
2014-12-01 15:00:00
2014-12-01 14:00:00
2014-12-01 14:00:00
2014-12-01 13:00:00
2014-12-01 13:00:00
2014-12-01 13:00:00
2014-12-01 12:00:00
2014-12-01 12:00:00
2014-12-01 12:00:00
2014-12-01 11:00:00
2014-12-01 11:00:00
2014-12-01 11:00:00
2014-12-01 10:00:00
2014-12-01 10:00:00
2014-12-01 10:00:00
2014-12-01 09:00:00
2014-12-01 09:00:00
2014-12-01 08:00:00
2014-12-01 08:00:00
2014-12-01 08:00:00
2014-12-01 08:00:00
2014-12-01 07:00:00
2014-12-01 07:00:00
...

for recalculation I use a script:
date_dif=`cat ./date_dif.txt`

for myI in ./PERIOD
    do
    co1=0
  while read line
        do
      case $line in
          ????-??-?????:??:*)
          line=`echo "$line" | awk '{print $1 " " $2}'`
          date_line_f[$co1]=$line
          line=`date --date="$line" +%s`
          let line=$line+$date_dif
          line=`date [email protected]$line +"%F %H:%M:%S"`
          date_line_s[$co1]=$line
          sed -i "s#${date_line_f[$co1]}#${date_line_s[$co1]}#" ./$myI
      esac
        let co1=$co1+1
        done <./$myI
    done

and I get this nonsense:
TS USER
2014-12-05 18:30:00
2014-12-05 18:30:00
2014-12-05 18:25:00
2014-12-05 18:20:00
2014-12-05 18:20:00
2014-12-05 18:15:00
2014-12-05 18:15:00
2014-12-05 18:10:00
2014-12-05 18:10:00
...
2014-12-04 04:00:00
2014-12-04 04:00:00
2014-12-04 04:00:00
2014-12-04 03:00:00
2014-12-04 03:00:00
2014-12-04 02:00:00
2014-12-04 02:00:00
2014-12-04 02:00:00
2014-12-04 02:00:00
2014-12-04 01:00:00
2014-12-04 01:00:00
2014-12-04 01:00:00
2014-12-04 00:00:00
2014-12-04 00:00:00
2014-12-04 00:00:00
2014-12-03 21:00:00
2014-12-03 20:00:00
2014-12-03 20:00:00
2014-12-03 20:00:00
2014-12-03 19:00:00
2014-12-03 19:00:00
!!!!!!!!!! ВМЕСТО 2014-12-03 18:00:00 пересчитало черти что...
2014-12-05 18:00:00
2014-12-05 18:00:00
2014-12-05 18:00:00
2014-12-05 18:00:00
2014-12-05 17:00:00
2014-12-05 17:00:00
2014-12-05 17:00:00
2014-12-05 16:00:00
2014-12-05 16:00:00
2014-12-05 16:00:00
2014-12-05 15:00:00
2014-12-05 15:00:00
2014-12-05 15:00:00
2014-12-05 15:00:00
2014-12-05 14:00:00
2014-12-05 14:00:00
2014-12-05 13:00:00
2014-12-05 13:00:00
2014-12-05 13:00:00
2014-12-05 12:00:00
2014-12-05 12:00:00
2014-12-05 12:00:00
2014-12-05 11:00:00
2014-12-05 11:00:00
2014-12-05 11:00:00
2014-12-05 10:00:00
2014-12-05 10:00:00
2014-12-05 10:00:00
2014-12-05 09:00:00
2014-12-05 09:00:00
2014-12-05 08:00:00
2014-12-05 08:00:00
2014-12-05 08:00:00
2014-12-05 08:00:00
2014-12-05 07:00:00
2014-12-05 07:00:00
2014-12-05 07:00:00
2014-12-05 06:00:00
2014-12-05 06:00:00
2014-12-05 06:00:00
2014-12-05 05:00:00
2014-12-05 05:00:00
2014-12-05 05:00:00
!!!!!!!!!!!!!!!!!!!!!!!!! А ТЕПЕРЬ ОПЯТЬ СЧИТАЕТ НОРМАЛЬНО????
2014-12-03 04:00:00
2014-12-03 04:00:00
2014-12-03 04:00:00
2014-12-03 03:00:00
2014-12-03 03:00:00
...

at first, it seems that everything is correct (I cut out unnecessary entries everywhere because the limit was exceeded). Dates are recalculated taking into account the date_diff difference. But scrolling through, you can see that instead of the expected decrease in the date, there is such nonsense
2014-12-03 19:00:00
2014-12-05 18:00:00 (должно было быть 2014-12-03 18:00:00)
then spoiling a piece - the script makes at the end the appearance that everything worked out correctly. (here is the moment
2014-12-05 05:00:00
2014-12-03 04:00:00
). The entire date-calculating script is listed above. Here I am replacing the old date value with the new one
sed -i "s#${date_line_f[$co1]}#${date_line_s[$co1]}#" ./$myI
What is the problem - I can not understand! How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
3
3vi1_0n3, 2014-12-05
@ksvdon

Perhaps the buffer size of the pipe is not enough, I'm not sure. And in this case, the same result?

#!/bin/bash

date_dif=$(cat ./date_dif.txt)
myI=./PERIOD
exec 3<>$myI
    co1=0
    while read -u 3 line
        do
        case $line in
            ????-??-?????:??:*)
            date_line_f[$co1]=$line
            line=$(date --date="$line" +%s)
            let line=$line+$date_dif
            line=$(date [email protected]$line +"%F %H:%M:%S")
            date_line_s[$co1]=$line
            sed -i "s#${date_line_f[$co1]}#${date_line_s[$co1]}#" ./$myI
        esac
        let co1=$co1+1
        done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question