Answer the question
In order to leave comments, you need to log in
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
...
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
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
...
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 moment2014-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 onesed -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
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 questionAsk a Question
731 491 924 answers to any question