LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-19-2011, 01:18 PM   #1
sublimation
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:

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!
 
Old 05-19-2011, 02:33 PM   #2
lrtward
Member
 
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":
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

Last edited by lrtward; 05-19-2011 at 02:39 PM. Reason: clarifying explanation of what awk script is doing
 
1 members found this post helpful.
Old 05-19-2011, 02:44 PM   #3
sublimation
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!
 
  


Reply


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 10:59 AM
Linux Text File convert to Windows/Notepad Text File = Wrapped? backroger Linux - Software 4 01-18-2009 05:54 AM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 09: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 02:36 PM
LXer: The summing up LXer Syndicated Linux News 0 04-28-2006 05:21 PM


All times are GMT -5. The time now is 09:55 PM.

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