Add minutes to date time column

Clash Royale CLAN TAG#URR8PPP up vote
2
down vote
favorite
Thanks to various contributors resolving my previous request.
I need to add minutes to the date column to get new datetime stamp value. I have a file : afile.txt
1,2012-02-16,abc,aa,455,340
3,2015-02-16,dsa,dl,350,200
2,2015-02-16,aws,sw,555,180
4,2015-02-16,yyz,aa,1220,210
I have used awk (as provided to me by contributors earlier) --
awk -F, '/,/
printf "%s, %s, %s, %s, %s %02d:%02d, %sn",
$1, $2, $3, $4,
$2, int($5 / 100), $5 % 100,
$6
' afile.txt > bfile.txt
bfile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 340
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 200
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 180
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 210
I want to add column 5 and 6 and get the new timestamp.
Your help is much appreciated.
14.04 bash sed awk perl
add a comment |Â
up vote
2
down vote
favorite
Thanks to various contributors resolving my previous request.
I need to add minutes to the date column to get new datetime stamp value. I have a file : afile.txt
1,2012-02-16,abc,aa,455,340
3,2015-02-16,dsa,dl,350,200
2,2015-02-16,aws,sw,555,180
4,2015-02-16,yyz,aa,1220,210
I have used awk (as provided to me by contributors earlier) --
awk -F, '/,/
printf "%s, %s, %s, %s, %s %02d:%02d, %sn",
$1, $2, $3, $4,
$2, int($5 / 100), $5 % 100,
$6
' afile.txt > bfile.txt
bfile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 340
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 200
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 180
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 210
I want to add column 5 and 6 and get the new timestamp.
Your help is much appreciated.
14.04 bash sed awk perl
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Thanks to various contributors resolving my previous request.
I need to add minutes to the date column to get new datetime stamp value. I have a file : afile.txt
1,2012-02-16,abc,aa,455,340
3,2015-02-16,dsa,dl,350,200
2,2015-02-16,aws,sw,555,180
4,2015-02-16,yyz,aa,1220,210
I have used awk (as provided to me by contributors earlier) --
awk -F, '/,/
printf "%s, %s, %s, %s, %s %02d:%02d, %sn",
$1, $2, $3, $4,
$2, int($5 / 100), $5 % 100,
$6
' afile.txt > bfile.txt
bfile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 340
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 200
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 180
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 210
I want to add column 5 and 6 and get the new timestamp.
Your help is much appreciated.
14.04 bash sed awk perl
Thanks to various contributors resolving my previous request.
I need to add minutes to the date column to get new datetime stamp value. I have a file : afile.txt
1,2012-02-16,abc,aa,455,340
3,2015-02-16,dsa,dl,350,200
2,2015-02-16,aws,sw,555,180
4,2015-02-16,yyz,aa,1220,210
I have used awk (as provided to me by contributors earlier) --
awk -F, '/,/
printf "%s, %s, %s, %s, %s %02d:%02d, %sn",
$1, $2, $3, $4,
$2, int($5 / 100), $5 % 100,
$6
' afile.txt > bfile.txt
bfile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 340
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 200
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 180
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 210
I want to add column 5 and 6 and get the new timestamp.
Your help is much appreciated.
14.04 bash sed awk perl
edited May 27 at 0:01
steeldriver
62.1k1196163
62.1k1196163
asked May 26 at 23:52
I Singh
442
442
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
I'm assuming that the last field should be interpreted in the same fashion as the penultimate one i.e. 340 means an offset of 3 hours and 40 minutes.
If that is correct, then using GNU awk's Time Functions you could do
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(d[1]" "d[2]" "d[3]" "int($5 / 100)" "$5 % 100" 0");
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 3600 * int($6 / 100) + 60 * ($6 % 100))
1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 08:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 05:50
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:15
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 14:30
If the last field is simply minutes, then the math for $6 is easier:
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(sprintf("%d %d %d %d %d 0", d[1], d[2], d[3], int($5 / 100), $5 % 100));
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 60 * $6) 1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 10:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 07:10
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:55
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 15:50
My fault. The last line is number of minutes that needs to be added to 5th column (date : yyyy-mm-dd hh:mm).
â I Singh
May 27 at 2:28
@ISingh please see updated answer
â steeldriver
May 27 at 2:34
You are an angel. Thank you so much for helping me so quickly.
â I Singh
May 27 at 2:36
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
I'm assuming that the last field should be interpreted in the same fashion as the penultimate one i.e. 340 means an offset of 3 hours and 40 minutes.
If that is correct, then using GNU awk's Time Functions you could do
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(d[1]" "d[2]" "d[3]" "int($5 / 100)" "$5 % 100" 0");
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 3600 * int($6 / 100) + 60 * ($6 % 100))
1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 08:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 05:50
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:15
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 14:30
If the last field is simply minutes, then the math for $6 is easier:
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(sprintf("%d %d %d %d %d 0", d[1], d[2], d[3], int($5 / 100), $5 % 100));
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 60 * $6) 1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 10:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 07:10
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:55
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 15:50
My fault. The last line is number of minutes that needs to be added to 5th column (date : yyyy-mm-dd hh:mm).
â I Singh
May 27 at 2:28
@ISingh please see updated answer
â steeldriver
May 27 at 2:34
You are an angel. Thank you so much for helping me so quickly.
â I Singh
May 27 at 2:36
add a comment |Â
up vote
5
down vote
I'm assuming that the last field should be interpreted in the same fashion as the penultimate one i.e. 340 means an offset of 3 hours and 40 minutes.
If that is correct, then using GNU awk's Time Functions you could do
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(d[1]" "d[2]" "d[3]" "int($5 / 100)" "$5 % 100" 0");
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 3600 * int($6 / 100) + 60 * ($6 % 100))
1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 08:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 05:50
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:15
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 14:30
If the last field is simply minutes, then the math for $6 is easier:
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(sprintf("%d %d %d %d %d 0", d[1], d[2], d[3], int($5 / 100), $5 % 100));
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 60 * $6) 1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 10:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 07:10
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:55
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 15:50
My fault. The last line is number of minutes that needs to be added to 5th column (date : yyyy-mm-dd hh:mm).
â I Singh
May 27 at 2:28
@ISingh please see updated answer
â steeldriver
May 27 at 2:34
You are an angel. Thank you so much for helping me so quickly.
â I Singh
May 27 at 2:36
add a comment |Â
up vote
5
down vote
up vote
5
down vote
I'm assuming that the last field should be interpreted in the same fashion as the penultimate one i.e. 340 means an offset of 3 hours and 40 minutes.
If that is correct, then using GNU awk's Time Functions you could do
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(d[1]" "d[2]" "d[3]" "int($5 / 100)" "$5 % 100" 0");
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 3600 * int($6 / 100) + 60 * ($6 % 100))
1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 08:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 05:50
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:15
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 14:30
If the last field is simply minutes, then the math for $6 is easier:
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(sprintf("%d %d %d %d %d 0", d[1], d[2], d[3], int($5 / 100), $5 % 100));
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 60 * $6) 1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 10:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 07:10
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:55
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 15:50
I'm assuming that the last field should be interpreted in the same fashion as the penultimate one i.e. 340 means an offset of 3 hours and 40 minutes.
If that is correct, then using GNU awk's Time Functions you could do
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(d[1]" "d[2]" "d[3]" "int($5 / 100)" "$5 % 100" 0");
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 3600 * int($6 / 100) + 60 * ($6 % 100))
1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 08:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 05:50
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:15
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 14:30
If the last field is simply minutes, then the math for $6 is easier:
awk '
BEGINFS=","; OFS=", "
split($2,d,"-");
t0 = mktime(sprintf("%d %d %d %d %d 0", d[1], d[2], d[3], int($5 / 100), $5 % 100));
$5 = strftime("%Y-%m-%d %H:%M", t0);
$6 = strftime("%Y-%m-%d %H:%M", t0 + 60 * $6) 1' afile.txt
1, 2012-02-16, abc, aa, 2012-02-16 04:55, 2012-02-16 10:35
3, 2015-02-16, dsa, dl, 2015-02-16 03:50, 2015-02-16 07:10
2, 2015-02-16, aws, sw, 2015-02-16 05:55, 2015-02-16 08:55
4, 2015-02-16, yyz, aa, 2015-02-16 12:20, 2015-02-16 15:50
edited May 27 at 2:33
answered May 27 at 1:16
steeldriver
62.1k1196163
62.1k1196163
My fault. The last line is number of minutes that needs to be added to 5th column (date : yyyy-mm-dd hh:mm).
â I Singh
May 27 at 2:28
@ISingh please see updated answer
â steeldriver
May 27 at 2:34
You are an angel. Thank you so much for helping me so quickly.
â I Singh
May 27 at 2:36
add a comment |Â
My fault. The last line is number of minutes that needs to be added to 5th column (date : yyyy-mm-dd hh:mm).
â I Singh
May 27 at 2:28
@ISingh please see updated answer
â steeldriver
May 27 at 2:34
You are an angel. Thank you so much for helping me so quickly.
â I Singh
May 27 at 2:36
My fault. The last line is number of minutes that needs to be added to 5th column (date : yyyy-mm-dd hh:mm).
â I Singh
May 27 at 2:28
My fault. The last line is number of minutes that needs to be added to 5th column (date : yyyy-mm-dd hh:mm).
â I Singh
May 27 at 2:28
@ISingh please see updated answer
â steeldriver
May 27 at 2:34
@ISingh please see updated answer
â steeldriver
May 27 at 2:34
You are an angel. Thank you so much for helping me so quickly.
â I Singh
May 27 at 2:36
You are an angel. Thank you so much for helping me so quickly.
â I Singh
May 27 at 2:36
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1040725%2fadd-minutes-to-date-time-column%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password