LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   summing durations from a text file (https://www.linuxquestions.org/questions/linux-newbie-8/summing-durations-from-a-text-file-881638/)

sublimation 05-19-2011 01:18 PM

summing durations from a text file
 
So thanks to a little bit of pipe action and using mediainfo I was able to create a text file with the durations of recordings in the following format:

Code:

Duration                        : 13mn 16s
Duration                        : 1h 57mn
Duration                        : 1h 33mn
Duration                        : 47mn 1s
Duration                        : 45mn 51s
Duration                        : 2h 13mn

...etc

I'd like to know just long all of this is. I was originally thinking of doing several sed commands creating additional text files from the 'h' 'mn' and 's' throughout, but I feel like that's creating more work for myself than it needs to be.

I feel like awk should do this, but I have no idea how to work awk, so any help would be appreciated!

lrtward 05-19-2011 02:33 PM

There is almost certainly a more elegant way, but here's one that works, at least.
First, separate the numbers from their labels, so awk sees them as different fields.
Assuming your file above is called "myfile":
Code:

# sed -e 's/mn/ mn/' myfile | sed -e 's/s/ s/' | sed -e 's/h/ h/' > timefile
# cat timefile
Duration                        : 13 mn 16 s
Duration                        : 1 h 57 mn
Duration                        : 1 h 33 mn
Duration                        : 47 mn 1 s
Duration                        : 45 mn 51 s
Duration                        : 2 h 13 mn

Then make an awk script to parse the file.
Code:

# cat awk.tst
$4 ~ /mn/ { minutes += $3 }

$4 ~ /h/ { hours += $3 }

$6 ~ /mn/ { minutes += $5 }

$6 ~ /s/ { seconds += $5 }

END {
        print hours " hours"
        print minutes " minutes"
        print seconds " seconds"}

What this is doing is it's saying if "mn" is found in the 4th field, increment the variable minutes by whatever number is in the third field.
If "h" is found in the 4th field, increment the variable hours by whatever number is in the third field.
Then it looks for "mn" and "s" in the 6th field, incrementing the variables "minutes" and "seconds" appropriately.

The END portion of an awk script is executed after all lines of the input file have been parsed. In this case, the END section simply prints out all the totals you've calculated.

You can execute the awk file by running the following command:
Code:

# awk -f awk.tst timefile
4 hours
208 minutes
68 seconds


sublimation 05-19-2011 02:44 PM

Hooray, that took care of it.
Doing the adding myself, I have a folder full of 194 hours 43 minutes of recordings.

And that demystifies some of AWK for me too. I appreciate the explanation.

Many thanks lrtward!


All times are GMT -5. The time now is 10:45 PM.