need to split "date" command ouput in shell - linux

I want to split date command output and extract time zone difference and represent that time difference in terms of 30 min.
I tried this:
date -R | awk 'NF>1{print $NF}'
I got the output -0530
This means 5 hours and 30 min difference from GMT. Now I want to convert this -0530 in to -11. So what logic should I use in shell command?

date -R | awk '{tz=$NF;ew=substr(tz,1,1);h=substr(tz,1,3)*2;m=ew substr(tz,4,2)/30;print h + m}'
https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
Edited to work with omitted sign
date -R | awk '{tz=$NF;l=length(tz);m=substr(tz,l-1,2)/30;h=substr(tz,l-3,2)*2;ew=l>4?ew=substr(tz,1,1):"+";print (ew h)+(ew m)}'

No need to complicate things.
tz=$(date '+%z'); echo "${tz:0:1}$(( 2 * ${tz:1:2} + ${tz:3} ))"

I wouldn't, and instead use a different tool. In the perl module Time::Piece you have tzoffset which returns the current timezone skew in seconds.
Thus:
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
$ENV{'TZ'}='GMT-6';
my $now = localtime;
#find number of 30m differences;
print $now -> tzoffset / 60 / 30;
You can also use variants of strftime to print time formatted to your liking:
print $now -> strftime("%c");
This can be cut down into a one liner if so desired:
perl -MTime::Piece 'print localtime -> tzoffset / 60 / 30'

With GNU date, you could perform math on time/date values (in UTC):
$ date -uR -d "now"; date -uR -d "now -60 min"
Sun, 27 Dec 2015 20:34:17 +0000
Sun, 27 Dec 2015 19:34:17 +0000
A time of -5:30 is -330 min:
$ date -uR -d "now"; date -uR -d "now -330 min"
Sun, 27 Dec 2015 20:34:55 +0000
Sun, 27 Dec 2015 15:04:55 +0000
That could be further moved by the time zone:
$ TZ=America/New_York date -R -d "now"; TZ=America/New_York date -R -d "now -330 min"
Sun, 27 Dec 2015 15:34:26 -0500
Sun, 27 Dec 2015 10:04:26 -0500
That is the local time (now) subtracting 5 hours and 30 minutes.
However, what is a time zone of -5:30?
I know that India is +5:30 and there are some (very few) places that are +11:00.
But I fail to find any -5:30 and -11:00 seems just odd.

Related

How to add an interval to a date/time stored in a variable

I'm writing a bash script on a Fedora 27 machine. This script runs a Python program at intervals, displaying occasional progress messages. It's working except for a line that adds an interval to a time stored in a variable.
Does anyone know how to accomplish this?
Here's a minimal version of the script.
#!/usr/bin/env bash
next_run_dat=${1}
echo -n 'Next run will be at: ';echo ${next_run_dat}
now_dat=`date +'%Y-%m-%d %H:%M:%S'`
echo -n 'The time is now: ';echo ${now_dat}
while [[ ${now_dat} < ${next_run_dat} ]]
do
sleep 10
now_dat=`date +'%Y-%m-%d %H:%M:%S'`
echo -n 'The time is now: ';echo ${now_dat}
done
while true
do
echo 'This line represents a run.'
sleep 5
# ==> PROBLEM LINE BELOW <==
next_run_dat=$(date -d "${next_run_dat} + 25 seconds" +'Y-%m-%d %H:%M:%S')
echo -n 'Next run will be at: ';echo ${next_run_dat}
now_dat=`date +'%Y-%m-%d %H:%M:%S'`
echo -n 'The time is now: ';echo ${now_dat}
while [[ ${now_dat} < ${next_run_dat} ]]
do
sleep 5
now_dat=`date +'%Y-%m-%d %H:%M:%S'`
echo -n 'The time is now: ';echo ${now_dat}
done
done
And here's the output from running the minimal version:
$ bash control_bash_minimal.sh '2018-07-23 09:38:00'
Next run will be at: 2018-07-23 09:38:00
The time is now: 2018-07-23 09:37:36
The time is now: 2018-07-23 09:37:46
The time is now: 2018-07-23 09:37:56
The time is now: 2018-07-23 09:38:06
This line represents a run.
date: invalid date ‘2018-07-23 09:38:00 + 25 seconds’
Next run will be at:
The time is now: 2018-07-23 09:38:11
This line represents a run.
Next run will be at: Y-07-23 09:38:41
The time is now: 2018-07-23 09:38:16
^C
Thanks very much in advance for any help with this problem.
The input format uses + for two different purposes: to specify a timezone in a timestamp, and to specify a relative increase to a timestamp. In your case, date is trying to parse 25 seconds as a timezone. If you specify an explicit timezone, then you can add an offset:
$ date -d "2018-07-23 09:38:00"
Mon Jul 23 09:38:00 EDT 2018
$ date -d "2018-07-23 09:38:00 + 25 seconds"
date: invalid date ‘2018-07-23 09:38:00 + 25 seconds’
# I used a minus sign here to make the output match
# the first example...
$ date -d "2018-07-23 09:38:00-0400 + 25 seconds"
Mon Jul 23 09:38:25 EDT 2018
# ... but positive timezone offsets work too
# (Note that date uses the timezone to parse the
# input, but converts the result to your local
# timezone.)
$ date -d "2018-07-23 09:38:00+0100 + 25 seconds"
Mon Jul 23 04:38:25 EDT 2018
There may be a way to disambiguate without adding an explicit timezone that I am unaware of.
note: this is more an extended comment to chepner's answer
From the manual of GNU coreutils:
Combined date and time of day items
The ISO 8601 date and time of day extended format consists of an ISO 8601 date, a ‘T’ character separator, and an ISO 8601 time of day. This format is also recognized if the ‘T’ is replaced by a space.
In this format, the time of day should use 24-hour notation. Fractional seconds are allowed, with either comma or period preceding the fraction. ISO 8601 fractional minutes and hours are not supported. Typically, hosts support nanosecond timestamp resolution; excess precision is silently discarded.
Sadly enough they make no mention of whether or not a time-zone should be included.
Trying to disentangle the source code and the used GNUlib gives me the feeling that chepner is correct. The double usage of the sign brakes the date parser. To be more correct, it assumes that the first number after + or - is a time-zone offset in hours. Normally, time zones have the format +HH:MM or -HH:MM, but a single number implements it as +HH:00. Evidently, the number has to be smaller than or equal to 24. Example:
$ TZ=UTC date -d "2018-07-23T09:38:00 + 9 seconds"
Mon 23 Jul 00:38:01 UTC 2018
$ TZ=UTC date -d "2018-07-23T09:38:00 + 9 2 seconds"
Mon 23 Jul 00:38:02 UTC 2018
Here, the date is assumed to be in UTC+09:00 and converted to UTC and incremented with a single second and in the second case two seconds.
The example of the OP fails because + 25 seconds is assumed to be UTC+25:00, but this is an invalid time zone:
$ date -d "2018-07-23T09:38:00 + 25 seconds"
date: invalid date ‘2018-07-23T09:38:00 + 25 seconds’
So, how can we add relative times without falling into the TZ-trap?
The date parser expects a signed or unsigned number for relative times. Hence we don't really need to add the plus sign and thus we can exploit this for adding time by removing the + sign:
$ date -d "2018-07-23T09:38:00 25 seconds"
Mon 23 Jul 09:38:25 UTC 2018
This however only works when you add relative time and not when you subtract it. But again, we can trick the parser by adding first ZERO seconds or hours or days or whatever to it:
$ date -d "2018-07-23T09:38:00 - 25 seconds"
date: invalid date ‘2018-07-23T09:38:00 - 25 seconds’
$ date -d "2018-07-23T09:38:00 0 hours - 25 seconds"
Mon 23 Jul 09:37:35 UTC 2018
You can also make use of the keywords next and prev:
$ date -d "2018-07-23T09:38:00 next 25 seconds"
Mon 23 Jul 09:38:25 UTC 2018
$ date -d "2018-07-23T09:38:00 prev 25 seconds"
Mon 23 Jul 09:37:35 UTC 2018
If the time zone if not really of importance, simply work in UTC, just add a Z to the end of the string.
$ date -d "2018-07-23T09:38:00Z + 25 seconds"
Mon 23 Jul 09:38:25 UTC 2018
But the easiest of all is to use float-numbers. As time-zones timezones are given as HH:MM a float cannot be interpreted as a time-zone and thus
$ date -d "2018-07-23T09:38:00 + 25.0 seconds"
Mon 23 Jul 09:38:25 UTC 2018
$ date -d "2018-07-23T09:38:00 - 25.0 seconds"
Mon 23 Jul 09:37:35 UTC 2018
You can convert date into EPOCH seconds and add no of seconds using BASH arithmetic like this:
dt='2018-07-23 09:38:00'
newdt=$(date -d "#$(( $(date -d "$dt" +%s) + 25))" +'%Y-%m-%d %H:%M:%S')
echo "$newdt"
2018-07-23 09:38:25

With date, how to specify a relative date when the specified date contains hours/minutes/seconds

In Bash, I want to compute the sum of a date (reception date) and a relative duration (retention duration).
For example, let :
reception_date="2017-01-02 12:34:56"
retention_duration="+2 days"
I expect to have:
expiration_date="2017-01-04--12-34-56"
Please note that the retention duration could be any valid relative date recognize by date, like +X weeks or +X hours.
My first grasp of the date info page lead me to use date like this
$ date -d '2017-01-02 12:34:56' +'%Y-%m-%d--%H-%M-%S'
2017-01-02--12-34-56
$ date -d '2017-01-02 12:34:56 +1 days' +'%Y-%m-%d--%H-%M-%S'
2017-01-03--12-34-56
$ date -d '2017-01-02 12:34:56 +2 days' +'%Y-%m-%d--%H-%M-%S'
2017-01-03--11-34-56
$ date -d '2017-01-02 12:34:56 +3 days' +'%Y-%m-%d--%H-%M-%S'
2017-01-03--10-34-56
$ date -d '2017-01-02 12:34:56 +4 days' +'%Y-%m-%d--%H-%M-%S'
2017-01-03--09-34-56
$ date -d '2017-01-02 12:34:56 +5 days' +'%Y-%m-%d--%H-%M-%S'
2017-01-03--08-34-56
As you can see, it seems to work fine for the +1 days but I get really irregular results for higher values.
The following example work as expected but are far less readable:
$ date -d '2017-01-02 +1 days 12:34:56' +'%Y-%m-%d--%H-%M-%S'
2017-01-03--12-34-56
$ date -d '2017-01-02 +2 days 12:34:56' +'%Y-%m-%d--%H-%M-%S'
2017-01-04--12-34-56
$ date -d '2017-01-02 +3 days 12:34:56' +'%Y-%m-%d--%H-%M-%S'
2017-01-05--12-34-56
$ date -d '+1 days 2017-01-02 12:34:56' +'%Y-%m-%d--%H-%M-%S'
2017-01-03--12-34-56
$ date -d '+2 days 2017-01-02 12:34:56' +'%Y-%m-%d--%H-%M-%S'
2017-01-04--12-34-56
$ date -d '+3 days 2017-01-02 12:34:56' +'%Y-%m-%d--%H-%M-%S'
2017-01-05--12-34-56
In the ideal solution, I would like to have :
the correct expiration_date
the readable relative date format (to explain to the user how and why this expiration date was computed).
I don't want to play with seconds conversions and stuff like that, the human-level readability of the solution is mandatory.
Thanks !
More info:
I'm on a RedHat, with date version:
$ date --version
date (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.
Revised answer
Following on from this downstream bug-report comment, I find you can do this by adding now:
$ date -d '2017-01-02 12:34:56 +2 days' +'%Y-%m-%d--%H-%M-%S'
2017-01-03--05-34-56 ## Oops
$ date -d '2017-01-02 12:34:56 now +2 days' +'%Y-%m-%d--%H-%M-%S'
2017-01-04--12-34-56 ## Much better
The now prevents date from parsing +2 as a timezone specification as well as a relative item. The difference between the two is explained in the docs, but the parser is apparently a bit odd in this situation. I think "now" also reads oddly from a human standpoint, but it could be worse!
To see what date is doing, add the --debug flag to your date command. See below for full explanation.
Original answer
At least as of now (and still as of date version 8.26), you have to put the relative time items somewhere other than after the times. The problem is that the + 1 hour is being interpreted both as a relative time and as a timezone (!!!!). The --debug flag to date is your friend.
Example of the right behavior (cygwin, date v8.26; my comments added):
$ date --debug -d '+1 hour 2017-01-19 12:34:56' ## Relative before the time
date: parsed relative part: +1 hour(s) ## OK
date: parsed date part: (Y-M-D) 2017-01-19
date: parsed time part: 12:34:56
date: input timezone: -05:00 (set from TZ="America/New_York" environment value) ## Also OK
date: using specified time as starting value: '12:34:56'
date: starting date/time: '(Y-M-D) 2017-01-19 12:34:56 TZ=-05:00'
date: '(Y-M-D) 2017-01-19 12:34:56 TZ=-05:00' = 1484847296 epoch-seconds
date: after time adjustment (+1 hours, +0 minutes, +0 seconds, +0 ns),
date: new time = 1484850896 epoch-seconds
date: output timezone: -05:00 (set from TZ="America/New_York" environment value) ## Still OK
date: final: 1484850896.000000000 (epoch-seconds)
date: final: (Y-M-D) 2017-01-19 18:34:56 (UTC0)
date: final: (Y-M-D) 2017-01-19 13:34:56 (output timezone TZ=-05:00)
Thu, Jan 19, 2017 1:34:56 PM ## A sensible result
Example of the wrong behavior:
$ date --debug -d '2017-01-19 12:34:56 + 1 hour' # +1 hour at the _end_
date: parsed date part: (Y-M-D) 2017-01-19
date: parsed time part: 12:34:56 TZ=+01:00 ## TZ=+1 oops!
date: parsed relative part: +1 hour(s) ## Parsed as a relative part also!
date: input timezone: +01:00 (set from parsed date/time string) ##Oops
date: using specified time as starting value: '12:34:56'
date: starting date/time: '(Y-M-D) 2017-01-19 12:34:56 TZ=+01:00' ##Oops
date: '(Y-M-D) 2017-01-19 12:34:56 TZ=+01:00' = 1484825696 epoch-seconds
date: after time adjustment (+1 hours, +0 minutes, +0 seconds, +0 ns),
date: new time = 1484829296 epoch-seconds
date: output timezone: -05:00 (set from TZ="America/New_York" environment value) ## That's OK
date: final: 1484829296.000000000 (epoch-seconds)
date: final: (Y-M-D) 2017-01-19 12:34:56 (UTC0)
date: final: (Y-M-D) 2017-01-19 07:34:56 (output timezone TZ=-05:00)
Thu, Jan 19, 2017 7:34:56 AM ## Bogus!
I am guessing that you are in a UTC+1 time zone, so date(1) interpreting +1 days as a UTC+1:00 timezone didn't hurt you. Edit This has been reported to gnulib but does not appear to have been patched, so you may just have to go with the workaround you have already hit on. The good news is that you do not have to present the same strings to users that you do to date. For example,
rel="+2 days"
base="$(date)"
exp="$(date -d "$rel $base")
echo "$base $rel is $exp"
Note the order of $base and $rel is swapped in the last two lines.
Another alternative, of course, is to switch everything to UTC so you can say date --debug -d '2017-01-19 12:34:56 Z + 1 hour' (with Z) to force the relative part to be interpreted as such.

Get the next time occurance with linux date

Linux date utility can understand a lot of strings including for instance:
$ date -d '8:30'
Fri Jan 2 08:30:00 CET 2015
I'm looking for a way to get the next 8:30, thus:
in case it is Fri Jan 2 before 8:30, the result above should be returned;
otherwise it should print Sat Jan 3 08:30:00 CET 2015.
As one can see next 8:30 doesn't result in the correct answer:
$ date -d 'next 8:30'
date: invalid date ‘next 8:30’
Is there a single expression to calculate this?
Handling it in the shell oneself is of course an option, but makes things more complicates because of daylight save time regulation etc.
In case the clock is adapted to daylight save time, next 8:30 should be parsed to 8:30 according to the settings of the next day.
Testcase:
Given it is Fri Jan 2 12:01:01 CET 2015, the result should be:
$ date -d 'next 8:30'
Sat Jan 3 08:30:00 CET 2015
$ date -d 'next 15:30'
Fri Jan 2 15:30:00 CET 2015
Just use something like:
if [[ $(date -d '8:30 today' +%s) -lt $(date +%s) ]] ; then
next830="$(date -d '8:30 tomorrow')"
else
next830="$(date -d '8:30 today')"
fi
The %s format string gives you seconds since the epoch so the if statement is basically:
if 8:30-today is before now:
use 8:30-tomorrow
else
use 8:30-today
I researched and it does not seem to be possible to do so.
What you can probably do is to compare the hour and minute with 830 and print accordingly:
[ $(date '+%H%M') -le 830 ] && date -d '8:30' || date -d '8:30 + 1 day'
In case you want to work with this easily, create a function to do these calculations.
Test
$ [ $(date '+%H%M') -le 830 ] && date '8:30' || date -d '8:30 + 1 day'
Sat Jan 3 08:30:00 CET 2015

Configuring date command to meet my format

I have a date in YYYY.MM.DD HH:SS format (e.g. 2014.02.14 13:30). I'd like to convert it in seconds since epoch using the date command.
The command
date -d"2014.02.14 13:30" +%s
won't work, because of the dots separation.
Any Ideas?
Why don't you make the date format acceptable? Just replace dots with dashes:
$ date --date="`echo '2014.02.14 13:30' | sed 's/\./-/g'`" +%s
1392370200
Here I first change the format:
$ echo '2014.02.14 13:30' | sed 's/\./-/g'
2014-02-14 13:30
and then use the result as a parameter for date.
Note that the result depends on your timezone.
You can use:
s='2014.02.14 13:30'
date -d "${s//./}"
Fri Feb 14 13:30:00 EST 2014
To get EPOCH value:
date -d "${s//./}" '+%s'
1392402600
using awk :
s=`echo "2014.02.14 13:30" | awk '{gsub(/\./,"-",$0);print $0}'`
echo -d "$s"
date -d "$s" +%s
output:
Fri Feb 14 13:30:00 IST 2014
1392364800
Perl: does not require you to munge the string
d="2014.02.14 13:30"
epoch_time=$(perl -MTime::Piece -E 'say Time::Piece->strptime(shift, "%Y.%m.%d %H:%M")->epoch' "$d")
echo $epoch_time
1392384600
Timezone: Canada/Eastern
I Finally solved it using
awk 'BEGIN{FS=","}{ gsub(/./," ",$1);gsub(/:/," ",$2); var=sprintf("%s %s 00",$1,$2); print mktime(var), $3,$4,$5,$6,$7 }' myfile | less
so myfile:
2014.09.24,15:15,1.27921,1.27933,1.279,1.27924,234
became
1411582500 1.27921 1.27933 1.279 1.27924 234
:)

Replace strings with evaluated string based on matched group (elegant way, not using for .. in)

I'm looking for a way to replace strings of a file, matched by a regular expression, with another string that will be generated/evaluated out of the matched string.
For example, I want to replace the timestamps (timestamp + duration) in this file
1357222500 3600 ...
Maybe intermediate strings...
1357226100 3600 ...
Maybe intermediate strings...
...
By human readable date representations (date range).
Until now, I always used shell scripts like Bash to iterate over each line, matching for the line X, getting the matched group string and printing the line after processing, for example this way (from memory):
IFS="
"
for L in `cat file.txt`; do
if [[ "${L}" =~ ^([0-9]{1,10})\ ([0-9]{1,4})\ .*$ ]]; then
# Written as three lines for better readability/recognition
echo -n "`date --date=#${BASH_REMATCH[1]}` - "
echo -n "`date --date=#$(( ${BASH_REMATCH[1]} + ${BASH_REMATCH[2]} ))`"
echo ""
else
echo "$L"
fi
done
I wonder if there's something like this with a fictional(?) "sed-2.0":
cat file.txt | sed-2.0 's+/^\([0-9]\{1,10\}\) \([0-9]\{1,4\}\) .*$+`date --date="#\1"` - `date --date="#$(( \1 + \2 ))`'
Whereas the backticks in the sed-2.0 replacement will be evaluated as shell command passing the matched groups \1 and \2.
I know that this does not work as expected, but I'd like to write someting like this.
Edit 1
Edit of question above: added missing echo "" in if of Bash script example.
This should be the expected output:
Do 3. Jan 15:15:00 CET 2013 - Do 3. Jan 16:15:00 CET 2013
Maybe intermediate strings...
Do 3. Jan 16:15:00 CET 2013 - Do 3. Jan 17:15:00 CET 2013
Maybe intermediate strings...
...
Note, that the timestamp depends on the timezone.
Edit 2
Edit of question above: fixed syntax error of Bash script example, added comment.
Edit 3
Edit of question above: fixed syntax error of Bash script example. Changed the phrase "old-school example" to "Bash script example".
Summary of Kent's and glenn jackman's answer
There's a huge difference in both approaches: the execution time. I've compared all four methods, here are the results:
gawk using strftime()
/usr/bin/time gawk '/^[0-9]+ [0-9]+ / {t1=$1; $1=strftime("%c -",t1); $2=strftime("%c",t1+$2)} 1' /tmp/test
...
0.06user 0.12system 0:00.30elapsed 60%CPU (0avgtext+0avgdata 1148maxresident)k
0inputs+0outputs (0major+327minor)pagefaults 0swaps
gawk using execution through getline (Gnu AWK Manual)
/usr/bin/time gawk '/^[0-9]{1,10} [0-9]{1,4}/{l=$1+$2; "date --date=#"$1|getline d1; "date --date=#"l|getline d2;print d1" - "d2;next;}1' /tmp/test
...
1.89user 7.59system 0:10.34elapsed 91%CPU (0avgtext+0avgdata 5376maxresident)k
0inputs+0outputs (0major+557419minor)pagefaults 0swaps
Custom Bash script
./sed-2.0.sh /tmp/test
...
3.98user 10.33system 0:15.41elapsed 92%CPU (0avgtext+0avgdata 1536maxresident)k
0inputs+0outputs (0major+759829minor)pagefaults 0swaps
sed using e option
/usr/bin/time sed -r 's#^([0-9]{1,10}) ([0-9]{1,4})(.*$)#echo $(date --date=#\1 )" - "$(date --date=#$((\1+\2)))#ge' /tmp/test
...
3.88user 16.76system 0:21.89elapsed 94%CPU (0avgtext+0avgdata 1272maxresident)k
0inputs+0outputs (0major+1253409minor)pagefaults 0swaps
Input data
for N in `seq 1 1000`; do echo -e "$(( 1357226100 + ( $N * 3600 ) )) 3600 ...\nSomething else ..." >> /tmp/test ; done
We can see that AWK using the strffime() method is the fastest. But even the Bash script is faster than sed with shell execution.
Kent showed us a more generic, universal way to accomplish what I've asked for. My question actually was not only limited to my timestamp example. In this case I had to do exactly this (replacing timestamp + duration by human readable date representation), but I had situations where I had to execute other code.
glenn jackman showed us a specific solution which is suitable for situations were you can do string operations and calculation directly in AWK.
So, it depends on the time you have (or time your script may run), the amount of the data and use case which method should be preferred.
based on your sample input:
gawk '/^[0-9]+ [0-9]+ / {t1=$1; $1=strftime("%c -",t1); $2=strftime("%c",t1+$2)} 1'
outputs
Thu 03 Jan 2013 09:15:00 AM EST - Thu 03 Jan 2013 10:15:00 AM EST ...
Maybe intermediate strings...
Thu 03 Jan 2013 10:15:00 AM EST - Thu 03 Jan 2013 11:15:00 AM EST ...
Maybe intermediate strings...
...
awk oneliner: (the datetime format could be different from your output)
awk '/^[0-9]{1,10} [0-9]{1,4}/{l=$1+$2; "date --date=#"$1|getline d1; "date --date=#"l|getline d2;print d1" - "d2;next;}1' file
test:
kent$ echo "1357222500 3600 ...
Maybe intermediate strings...
1357226100 3600 ...
Maybe intermediate strings...
..."|awk '/^[0-9]{1,10} [0-9]{1,4}/{l=$1+$2; "date --date=#"$1|getline d1; "date --date=#"l|getline d2;print d1" - "d2;next;}1'
Thu Jan 3 15:15:00 CET 2013 - Thu Jan 3 16:15:00 CET 2013
Maybe intermediate strings...
Thu Jan 3 15:15:00 CET 2013 - Thu Jan 3 17:15:00 CET 2013
Maybe intermediate strings...
...
Gnu sed
if you have gnu sed, the idea from your "not working" sed line could work in real world by applying gnu sed's s/foo/shell cmds/ge see below:
sed -r 's#^([0-9]{1,10}) ([0-9]{1,4})(.*$)#echo $(date --date=#\1 )" - "$(date --date=#$((\1+\2)))#ge' file
test
kent$ echo "1357222500 3600 ...
Maybe intermediate strings...
1357226100 3600 ...
Maybe intermediate strings...
..."|sed -r 's#^([0-9]{1,10}) ([0-9]{1,4})(.*$)#echo $(date --date=#\1 )" - "$(date --date=#$((\1+\2)))#ge'
Thu Jan 3 15:15:00 CET 2013 - Thu Jan 3 16:15:00 CET 2013
Maybe intermediate strings...
Thu Jan 3 16:15:00 CET 2013 - Thu Jan 3 17:15:00 CET 2013
Maybe intermediate strings...
...
if I would work on this, personally I would go with awk. because it is straightforward and easy to write.
at the end I paste my sed/awk version info :
kent$ sed --version|head -1
sed (GNU sed) 4.2.2
kent$ awk -V|head -1
GNU Awk 4.0.1

Resources