Calculate difference in numbers and save in colon-separated time format with bash script2019 Community Moderator ElectionHow to get elapsed time from two “text based” datesProcess Files to create a new fileConcatenate multiple fields separately based on one (key) columnRemoving numeric values in certain columns whilst keeping minus signs?Extract single information from a column in a fileRemove comma from columns in a file if it is at the endarithmetic operations within column with awk or sedArithmetic calculation row-wise for non-fixed column numberHow to process a column's multiple stringsHow to start a new column after every nth row?
PTIJ: Which Dr. Seuss books should one obtain?
Limit max CPU usage SQL SERVER with WSRM
How to test the sharpness of a knife?
Deciphering cause of death?
El Dorado Word Puzzle II: Videogame Edition
Do people actually use the word "kaputt" in conversation?
Showing mass murder in a kid's book
Did I make a mistake by ccing email to boss to others?
Is there a distance limit for minecart tracks?
Can you identify this lizard-like creature I observed in the UK?
Can I cause damage to electrical appliances by unplugging them when they are turned on?
Ways of geometrical multiplication
How to make a list of partial sums using forEach
If Captain Marvel (MCU) were to have a child with a human male, would the child be human or Kree?
Air travel with refrigerated insulin
What is the meaning of "You've never met a graph you didn't like?"
Why didn't Voldemort know what Grindelwald looked like?
How do I Interface a PS/2 Keyboard without Modern Techniques?
What does "tick" mean in this sentence?
Can I run 125kHz RF circuit on a breadboard?
How to make money from a browser who sees 5 seconds into the future of any web page?
When and why was runway 07/25 at Kai Tak removed?
How to leave product feedback on macOS?
Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?
Calculate difference in numbers and save in colon-separated time format with bash script
2019 Community Moderator ElectionHow to get elapsed time from two “text based” datesProcess Files to create a new fileConcatenate multiple fields separately based on one (key) columnRemoving numeric values in certain columns whilst keeping minus signs?Extract single information from a column in a fileRemove comma from columns in a file if it is at the endarithmetic operations within column with awk or sedArithmetic calculation row-wise for non-fixed column numberHow to process a column's multiple stringsHow to start a new column after every nth row?
I have a tab-separated file, e.g.,
0018803 01 1710 2050 002571
0018951 01 1934 2525 003277
0019362 02 2404 2415 002829
0019392 01 2621 2820 001924
0019542 01 2208 2413 003434
0019583 01 1815 2134 002971
The 3rd and 4th columns represent Start Time and End Time in HHMM format. If the Start Time or End Time passes midnight, it keeps incrementing the hours.
I want column 6 to show elapsed time of (Column 4 minus Column 3) in hours and minutes.
Column 6 result in this sample would be 3:40
, 5:51
, 00:11
, 1:59
, 2:05
.
shell-script text-processing date
New contributor
|
show 2 more comments
I have a tab-separated file, e.g.,
0018803 01 1710 2050 002571
0018951 01 1934 2525 003277
0019362 02 2404 2415 002829
0019392 01 2621 2820 001924
0019542 01 2208 2413 003434
0019583 01 1815 2134 002971
The 3rd and 4th columns represent Start Time and End Time in HHMM format. If the Start Time or End Time passes midnight, it keeps incrementing the hours.
I want column 6 to show elapsed time of (Column 4 minus Column 3) in hours and minutes.
Column 6 result in this sample would be 3:40
, 5:51
, 00:11
, 1:59
, 2:05
.
shell-script text-processing date
New contributor
1
What unit is are the numbers in column 3 and 4 using?
– Kusalananda
19 hours ago
@RoVo Well, in that case lines 2, 3, 4 and 5 are weird.
– Kusalananda
19 hours ago
there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4
– Aditya
19 hours ago
1
If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600
– Aditya
19 hours ago
1
are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123
– Dalvenjia
11 hours ago
|
show 2 more comments
I have a tab-separated file, e.g.,
0018803 01 1710 2050 002571
0018951 01 1934 2525 003277
0019362 02 2404 2415 002829
0019392 01 2621 2820 001924
0019542 01 2208 2413 003434
0019583 01 1815 2134 002971
The 3rd and 4th columns represent Start Time and End Time in HHMM format. If the Start Time or End Time passes midnight, it keeps incrementing the hours.
I want column 6 to show elapsed time of (Column 4 minus Column 3) in hours and minutes.
Column 6 result in this sample would be 3:40
, 5:51
, 00:11
, 1:59
, 2:05
.
shell-script text-processing date
New contributor
I have a tab-separated file, e.g.,
0018803 01 1710 2050 002571
0018951 01 1934 2525 003277
0019362 02 2404 2415 002829
0019392 01 2621 2820 001924
0019542 01 2208 2413 003434
0019583 01 1815 2134 002971
The 3rd and 4th columns represent Start Time and End Time in HHMM format. If the Start Time or End Time passes midnight, it keeps incrementing the hours.
I want column 6 to show elapsed time of (Column 4 minus Column 3) in hours and minutes.
Column 6 result in this sample would be 3:40
, 5:51
, 00:11
, 1:59
, 2:05
.
shell-script text-processing date
shell-script text-processing date
New contributor
New contributor
edited 9 hours ago
Jeff Schaller
43.8k1161141
43.8k1161141
New contributor
asked 20 hours ago
AdityaAditya
63
63
New contributor
New contributor
1
What unit is are the numbers in column 3 and 4 using?
– Kusalananda
19 hours ago
@RoVo Well, in that case lines 2, 3, 4 and 5 are weird.
– Kusalananda
19 hours ago
there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4
– Aditya
19 hours ago
1
If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600
– Aditya
19 hours ago
1
are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123
– Dalvenjia
11 hours ago
|
show 2 more comments
1
What unit is are the numbers in column 3 and 4 using?
– Kusalananda
19 hours ago
@RoVo Well, in that case lines 2, 3, 4 and 5 are weird.
– Kusalananda
19 hours ago
there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4
– Aditya
19 hours ago
1
If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600
– Aditya
19 hours ago
1
are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123
– Dalvenjia
11 hours ago
1
1
What unit is are the numbers in column 3 and 4 using?
– Kusalananda
19 hours ago
What unit is are the numbers in column 3 and 4 using?
– Kusalananda
19 hours ago
@RoVo Well, in that case lines 2, 3, 4 and 5 are weird.
– Kusalananda
19 hours ago
@RoVo Well, in that case lines 2, 3, 4 and 5 are weird.
– Kusalananda
19 hours ago
there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4
– Aditya
19 hours ago
there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4
– Aditya
19 hours ago
1
1
If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600
– Aditya
19 hours ago
If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600
– Aditya
19 hours ago
1
1
are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123
– Dalvenjia
11 hours ago
are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123
– Dalvenjia
11 hours ago
|
show 2 more comments
1 Answer
1
active
oldest
votes
Hi this assumes the start time and end time will always be 4 characters long:
awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'
Explanation:
NF
: Number of Fields, this is to skip empty lines
hrs = substr($4, 1, 2) - substr($3, 1, 2);
: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs
;
min = substr($4, 3, 4) - substr($3, 3, 4);
: Repeat the last logic with the last 2 characters for the minutes;
if(min<0) hrs--; min = 60 + min;
: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min
;
printf "%st%02d:%02dn", $0, hrs, min
: Lastly print all the line and the variables hrs
and min
zero padded
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
);
);
Aditya is a new contributor. Be nice, and check out our Code of Conduct.
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%2f507376%2fcalculate-difference-in-numbers-and-save-in-colon-separated-time-format-with-bas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Hi this assumes the start time and end time will always be 4 characters long:
awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'
Explanation:
NF
: Number of Fields, this is to skip empty lines
hrs = substr($4, 1, 2) - substr($3, 1, 2);
: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs
;
min = substr($4, 3, 4) - substr($3, 3, 4);
: Repeat the last logic with the last 2 characters for the minutes;
if(min<0) hrs--; min = 60 + min;
: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min
;
printf "%st%02d:%02dn", $0, hrs, min
: Lastly print all the line and the variables hrs
and min
zero padded
add a comment |
Hi this assumes the start time and end time will always be 4 characters long:
awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'
Explanation:
NF
: Number of Fields, this is to skip empty lines
hrs = substr($4, 1, 2) - substr($3, 1, 2);
: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs
;
min = substr($4, 3, 4) - substr($3, 3, 4);
: Repeat the last logic with the last 2 characters for the minutes;
if(min<0) hrs--; min = 60 + min;
: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min
;
printf "%st%02d:%02dn", $0, hrs, min
: Lastly print all the line and the variables hrs
and min
zero padded
add a comment |
Hi this assumes the start time and end time will always be 4 characters long:
awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'
Explanation:
NF
: Number of Fields, this is to skip empty lines
hrs = substr($4, 1, 2) - substr($3, 1, 2);
: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs
;
min = substr($4, 3, 4) - substr($3, 3, 4);
: Repeat the last logic with the last 2 characters for the minutes;
if(min<0) hrs--; min = 60 + min;
: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min
;
printf "%st%02d:%02dn", $0, hrs, min
: Lastly print all the line and the variables hrs
and min
zero padded
Hi this assumes the start time and end time will always be 4 characters long:
awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'
Explanation:
NF
: Number of Fields, this is to skip empty lines
hrs = substr($4, 1, 2) - substr($3, 1, 2);
: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs
;
min = substr($4, 3, 4) - substr($3, 3, 4);
: Repeat the last logic with the last 2 characters for the minutes;
if(min<0) hrs--; min = 60 + min;
: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min
;
printf "%st%02d:%02dn", $0, hrs, min
: Lastly print all the line and the variables hrs
and min
zero padded
edited 10 hours ago
answered 10 hours ago
DalvenjiaDalvenjia
1,22658
1,22658
add a comment |
add a comment |
Aditya is a new contributor. Be nice, and check out our Code of Conduct.
Aditya is a new contributor. Be nice, and check out our Code of Conduct.
Aditya is a new contributor. Be nice, and check out our Code of Conduct.
Aditya is a new contributor. Be nice, and check out our Code of Conduct.
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%2f507376%2fcalculate-difference-in-numbers-and-save-in-colon-separated-time-format-with-bas%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
1
What unit is are the numbers in column 3 and 4 using?
– Kusalananda
19 hours ago
@RoVo Well, in that case lines 2, 3, 4 and 5 are weird.
– Kusalananda
19 hours ago
there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4
– Aditya
19 hours ago
1
If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600
– Aditya
19 hours ago
1
are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123
– Dalvenjia
11 hours ago