Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ?

Thanks!

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
1  
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
add comment

1 Answer

up vote 4 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
add comment

Your Answer

 
discard

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.