Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-19-2011, 02:18 PM   #1
LQ Newbie
Registered: May 2011
Posts: 2

Rep: Reputation: 10
Post 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:

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

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!
Old 05-19-2011, 03:33 PM   #2
Registered: Feb 2011
Distribution: CentOS, Ubuntu
Posts: 97

Rep: Reputation: 9
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":
# 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.
# cat awk.tst
$4 ~ /mn/ { minutes += $3 }

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

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

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

        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:
# awk -f awk.tst timefile
4 hours
208 minutes
68 seconds

Last edited by lrtward; 05-19-2011 at 03:39 PM. Reason: clarifying explanation of what awk script is doing
1 members found this post helpful.
Old 05-19-2011, 03:44 PM   #3
LQ Newbie
Registered: May 2011
Posts: 2

Original Poster
Rep: Reputation: 10
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!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 11:59 AM
Linux Text File convert to Windows/Notepad Text File = Wrapped? backroger Linux - Software 4 01-18-2009 06:54 AM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 10:40 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM
LXer: The summing up LXer Syndicated Linux News 0 04-28-2006 06:21 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:09 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration