Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a list files containing random text and many datetimes in ISO format in it (e.g "2012-07-02T10:47:24+02:00"), how can I find all these datetimes and add 2 hours to each one ? Ideally a solution that takes into account the fact that a day is 24 hours would be great (i.e "02T23:15 + 2" should result in "03T01:15" and not "02T25:15") but in my case that's not a big deal. And let's ignore the possible timezone consequences, and consider this as a simple math operation.

I already know how to do this in Python for instance, reading all files line by line and replacing the line when datetimes are found but I wonder if there is a more straightforward way of doing this, with awk or sed maybe ?


share|improve this question
imho no as these programs afaik don't have a time component, so adding something will produce the wrong result – Ulrich Dangel Mar 22 '13 at 16:51
If you don't need the hour wraparound, you can use sed -i. Since sed can not do math, you'd need 24 patterns, each matching a specific hour and replacing with specific hour + 2. However, if all your strings contain the timezone offset (+02:00) you could simply cheat and change the time zone. :) – frostschutz Mar 22 '13 at 16:52
up vote 5 down vote accepted

It's more a job for perl like:

perl -MTime::Piece -pi -e 's/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d/
(Time::Piece->strptime($&,"%Y-%m-%dT%T")+2*3600)->datetime/ge' file1 file2...
share|improve this answer
Great ! That's exactly what I was looking for, thank you sir. – piouk Mar 26 '13 at 12:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.