How to get total time using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get total time using awk
# 1  
Old 07-22-2010
How to get total time using awk

Hi guys,

I can't find a solution to sum the h:m:s: columns.
Code:
28/05/2010  03h 29min 34seg ADSL TELEMAR
28/05/2010  12h 19min 21seg ADSL TELEMAR
29/05/2010  04h 20min 02seg ADSL TELEMAR
29/05/2010  04h 31min 45seg ADSL TELEMAR
30/05/2010  06h 10min 43seg ADSL TELEMAR

Thanks

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 07-22-2010 at 05:55 AM..
# 2  
Old 07-22-2010
Like this?
Code:
awk '{h+=substr($2,1,2); m+=substr($3,1,2); s+=substr($4,1,2)} END{print h,m,s}' infile
29 109 145

# 3  
Old 07-22-2010
Use below code

Use below code
Code:
awk ' BEGIN {toth=0;totm=0;tots=0;}{
toth=toth+substr($2,1,2);
totm=totm+substr($3,1,2);
tots=tots+substr($4,1,2);}
END{print "Total Hours : " toth ;
print "Total Min : " totm
print "Total Second : " tots}' file1

# 4  
Old 07-22-2010
Quote:
Originally Posted by zaxxon
Like this?
Code:
awk '{h+=substr($2,1,2); m+=substr($3,1,2); s+=substr($4,1,2)} END{print h,m,s}' infile
29 109 145

no need substr, awk can recognize the number.

Code:
awk '{h+=$2; m+=$3; s+=$4} END{print h,m,s}' infile

# 5  
Old 07-22-2010
I don't think awk can recognize number.

Quote:
Originally Posted by rdcwayx
no need substr, awk can recognize the number.

Code:
awk '{h+=$2; m+=$3; s+=$4} END{print h,m,s}' infile

This will give 0 0 0 only.substr is required.
# 6  
Old 07-22-2010
Quote:
Originally Posted by CaapAjayShukla
This will give 0 0 0 only.substr is required.
I confirm rdcwayx' post, awk doesn't need the substr.
Code:
awk '
{
  h += $2;
  m += $3;
  s += $4;
}
END {
  m += int(s / 60);
  s  = s % 60;
  h += int(m / 60);
  m  = m % 60;
  printf "%03dh %02dmin %02dsec\n", h, m, s;
}
' inputfile

Inputfile:
Code:
28/05/2010  03h 29min 34seg ADSL TELEMAR
28/05/2010  12h 19min 21seg ADSL TELEMAR
29/05/2010  04h 20min 02seg ADSL TELEMAR
29/05/2010  04h 31min 45seg ADSL TELEMAR
30/05/2010  06h 10min 43seg ADSL TELEMAR

Output:
Code:
030h 51min 25sec

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 7  
Old 07-22-2010
They are both correct - even awk on AIX (which isn't that versatile like GNU sed for example) can see the difference from numbers to letters without a delimeter between them. So leave out substr Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Total size utilizes by the files older than a time span

Through find command I identified the files older that 1 year. I need the overall size utilizes by these 1 year older files. Please share me the command to identify it .Thanks Please post in an adequate technical forum! (3 Replies)
Discussion started by: Sang
3 Replies

2. UNIX for Dummies Questions & Answers

Dig total query time?

I'm using a .txt file filled with domain names for dig to use, the problem is that when i look at the results I get the query time for each individual query, I want to know how long it took in total for all queries to run, how can I achieve this? any help would be greatly appreciated, thank you.... (3 Replies)
Discussion started by: r7a7v7
3 Replies

3. Shell Programming and Scripting

awk for total

Have a input file like this ..... attachments 100G shared 1T archive 300M documents 300G remotedocs 150M I need the total in GB's ... where as M=MB,G=GB,T=TB Basically need awk to calculate the total based on end... (5 Replies)
Discussion started by: greycells
5 Replies

4. Shell Programming and Scripting

Total time taken

Hi Friend, Need your help. I have a file which has information of start time and End time . I need to find how much time takes to complete the job . how can I do it in unix command . Example of Log file : Start Loading ---Thu Aug 2 17:14:09 EDT 2012 Load... (5 Replies)
Discussion started by: deep_kol
5 Replies

5. Shell Programming and Scripting

Total Count using AWK

Hi Everybody, I have the following example file... 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1... (8 Replies)
Discussion started by: sraj142
8 Replies

6. Shell Programming and Scripting

Total Time

Hello, I have one script which takes some time to complete. I Need the total exact time taken by this script. How can i modify this script. Regards, Sam. (21 Replies)
Discussion started by: j_panky
21 Replies

7. Shell Programming and Scripting

Awk total and variance

File1 0358 Not Visible ***:* NA:NA RDF1+TDEV Grp'd (M) RW 102413 0359 Not Visible ***:* NA:NA RDF1+TDEV N/Grp'd (m) RW - 035A Not Visible ***:* NA:NA RDF1+TDEV N/Grp'd (m) RW - 035B Not Visible ***:* NA:NA ... (2 Replies)
Discussion started by: greycells
2 Replies

8. Shell Programming and Scripting

awk total and print if

hi all! I have a space delimited file... I would like to total column 3 on the condition that column 2 is less than 1030 to a variable in my script something like this: TOTAL="`awk '{$2 < 1030} {s+=$3}END{print s}' file`" Seem to be ignoring the {$2 < 1030}, I'm not sure of the... (3 Replies)
Discussion started by: lyoncc
3 Replies

9. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies
Login or Register to Ask a Question