Calculate or sum timestamps between lines in awk
Clash Royale CLAN TAG#URR8PPP
I have the following input:
1. Track 01 05:21
2. Track 02 07:39
3. Track 03 04:27
I came up with the following script
awk -F: 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60' file
But the result I get is this:
0:01:21
0:04:00
0:07:27
In turn, with the same script, when I have a file with only the time and nothing else I get the correct output
Input:
05:21
07:39
04:27
Output:
0:05:21
0:13:00
0:17:27
awk
add a comment |
I have the following input:
1. Track 01 05:21
2. Track 02 07:39
3. Track 03 04:27
I came up with the following script
awk -F: 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60' file
But the result I get is this:
0:01:21
0:04:00
0:07:27
In turn, with the same script, when I have a file with only the time and nothing else I get the correct output
Input:
05:21
07:39
04:27
Output:
0:05:21
0:13:00
0:17:27
awk
add a comment |
I have the following input:
1. Track 01 05:21
2. Track 02 07:39
3. Track 03 04:27
I came up with the following script
awk -F: 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60' file
But the result I get is this:
0:01:21
0:04:00
0:07:27
In turn, with the same script, when I have a file with only the time and nothing else I get the correct output
Input:
05:21
07:39
04:27
Output:
0:05:21
0:13:00
0:17:27
awk
I have the following input:
1. Track 01 05:21
2. Track 02 07:39
3. Track 03 04:27
I came up with the following script
awk -F: 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60' file
But the result I get is this:
0:01:21
0:04:00
0:07:27
In turn, with the same script, when I have a file with only the time and nothing else I get the correct output
Input:
05:21
07:39
04:27
Output:
0:05:21
0:13:00
0:17:27
awk
awk
asked Mar 6 at 14:54
nylnyl
112
112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use two awk commands
awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'
the first awk extract the las field of the file and the second awk makes the procees you want.
this works whether the file contets only the time or not, as long as the time is the last field in the line
add a comment |
This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.
I would do
awk '
BEGIN h = m = s = 0
function add_time(min, sec)
s += sec
m += min
while (s >= 60) s -= 60; m++
while (m >= 60) m -= 60; h++
split($NF, t, /:/)
add_time(t[1], t[2])
printf "%d:%02d:%02dn", h, m, s
' file
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504730%2fcalculate-or-sum-timestamps-between-lines-in-awk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use two awk commands
awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'
the first awk extract the las field of the file and the second awk makes the procees you want.
this works whether the file contets only the time or not, as long as the time is the last field in the line
add a comment |
Use two awk commands
awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'
the first awk extract the las field of the file and the second awk makes the procees you want.
this works whether the file contets only the time or not, as long as the time is the last field in the line
add a comment |
Use two awk commands
awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'
the first awk extract the las field of the file and the second awk makes the procees you want.
this works whether the file contets only the time or not, as long as the time is the last field in the line
Use two awk commands
awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'
the first awk extract the las field of the file and the second awk makes the procees you want.
this works whether the file contets only the time or not, as long as the time is the last field in the line
answered Mar 6 at 15:16
Emilio GalarragaEmilio Galarraga
554410
554410
add a comment |
add a comment |
This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.
I would do
awk '
BEGIN h = m = s = 0
function add_time(min, sec)
s += sec
m += min
while (s >= 60) s -= 60; m++
while (m >= 60) m -= 60; h++
split($NF, t, /:/)
add_time(t[1], t[2])
printf "%d:%02d:%02dn", h, m, s
' file
add a comment |
This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.
I would do
awk '
BEGIN h = m = s = 0
function add_time(min, sec)
s += sec
m += min
while (s >= 60) s -= 60; m++
while (m >= 60) m -= 60; h++
split($NF, t, /:/)
add_time(t[1], t[2])
printf "%d:%02d:%02dn", h, m, s
' file
add a comment |
This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.
I would do
awk '
BEGIN h = m = s = 0
function add_time(min, sec)
s += sec
m += min
while (s >= 60) s -= 60; m++
while (m >= 60) m -= 60; h++
split($NF, t, /:/)
add_time(t[1], t[2])
printf "%d:%02d:%02dn", h, m, s
' file
This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.
I would do
awk '
BEGIN h = m = s = 0
function add_time(min, sec)
s += sec
m += min
while (s >= 60) s -= 60; m++
while (m >= 60) m -= 60; h++
split($NF, t, /:/)
add_time(t[1], t[2])
printf "%d:%02d:%02dn", h, m, s
' file
edited Mar 6 at 15:23
answered Mar 6 at 15:17
glenn jackmanglenn jackman
53k573114
53k573114
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504730%2fcalculate-or-sum-timestamps-between-lines-in-awk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown