The Inferno

It is a fallacy to state that something exists just because it can't be proven that it doesn't

  • Home

1

Feb

Just how long again?

Posted by Viren  Published in Text Processing

If you’ve ever wanted to see how long you’ll be playing a particular artist’s music for, if you were to play their entire discography, I present a one-liner in bash that will show you just that.

find . -type f -name '*.mp*' -exec exiftool '{}' + | grep Duration | awk '{x += $3; print x;}'

The venerable find command needs no introduction. Suffice it to say that the type switch restricts it to files, and the *.mp* restricts the files found to MP3s or MP2s.

Exiftool is a nifty command-line processing tool for tags of all kinds, as seen by its name. In this case, we want just the ‘Duration’ field of each song. Once we have those, we pipe those to awk and get a running total, which shows us how long the entire discography is when the final total is printed.

Now I know that I have 5668.14 minutes of Zappa goodness, or a mere 208 minutes of godly Death. You need to run this in the folder that has all the albums by the artist, or of course, you can adapt it to a script and pass in parameters and so on.

Tags: awk, bash, discography duration, exiftool, length, mp3, one-liner, shell script, Text Processing

no comment

14

Aug

She sells Shell Scripts on the Sea Shore

Posted by Viren  Published in Text Processing

Recently, I had to parse data in several text files and calculate averages. From these averages, I had to create a chart. So I could either write a program using a real language like C or Perl or something, or even worse, copy and paste each value into a spreadsheet and then go from there. You should be shuddering by now.

The data was in 5 different folders, with each folder containing 25 files, with the contents of each file being:

real 70.67
user 70.66
sys 0.00
real 70.82
user 70.81
sys 0.01
real 70.89
user 70.88
sys 0.00

What I needed was the average of the three lines with the word “real” in them. So, first we grep through all the files for the word real to get something like:

real 70.67
real 70.82
real 70.89
real 41.27
real 41.16
real 41.39
real 125.75
real 125.42

Now, we need to sum up every 3 lines and divide by 3 to get the average. Enter awk:

awk  ‘{x+=$2;if(!(NR%3)){printf(“%2.3f\n”,x/3);x=0}}’

What this does is to add the second column ($2) to a variable called x. Awk automatically initializes all variables to zero, so we don’t need to worry about bogus data. The NR variable holds the number of lines, so every time we pass three lines, divide the current sum by 3 and then reset the subtotal to zero. Perhaps, making the code a bit tidier might help, even though everyone loves those cryptic one liners:

awk  ‘{

x+=$2;

if(!(NR%3)){

printf(“%2.3f\n”,x/3);

x=0}

}’

Why, it’s almost C, I can hear you say.

Now, we stitch them together into one glorious command:

cat 2.30GHz/* | grep real | awk  ‘{x+=$2;if(!(NR%3)){printf(“%2.3f\n”,x/3);x=0}}’

We need to replace the 2.30GHz by a variable, so we can iterate through the folders. And we need to append the output to a file, to be imported into your favorite spreadsheet later. Here’s the final script

#!/bin/bash

freqs=( 2.30GHz 2.00GHz 1.70GHz 1.40GHz 1.15GHz )

for l in ${freqs[@]}
do
data=`cat $l/* | grep real | awk  ‘{x+=$2;if(!(NR%3)){printf(“%2.3f\n”,x/3);x=0}}’`
echo $data >> file.csv
done

And file.csv of course looks like:

198.967 265.083 543.800 139.247 51.973 70.793 41.273 125.640 214.127 220.863 91.303 15.230 46.397 256.093 176.000 178.037 213.133 183.947 31.743 181.223 220.143 192.857 47.360 82.017 177.790
224.363 295.043 601.253 150.720 58.730 81.213 47.243 132.723 244.263 244.343 103.090 17.160 53.183 273.963 188.223 201.857 238.343 209.247 36.773 207.530 235.840 223.670 53.480 91.600 196.297
256.523 316.210 676.660 165.870 67.860 95.867 55.793 138.647 279.900 273.450 118.363 19.590 62.377 293.080 199.717 236.997 271.997 242.750 42.667 244.317 253.797 259.940 61.300 102.497 220.560

…..

There you have it. Averages of all the required numbers from every file, all in one file. Import it as a space delimited file into Calc or Excel and Robert’s your mother’s brother.

Tags: awk, bash, linux, one-liner, shell script, Text Processing, ubuntu

no comment

Tags

american politics american right-wing anger beijing book Brutal Death Metal bullshit burnaby cartoon china christmas craigslist crossword death metal demotivational poster elections Funny Gentoo geopolitics graphic novel iran linguistics linux live music metal moustache music One-Armed Bandits pix pseudoscience quotes RCMP religion Republican revolution SATA satire Septoplasty setlist sfu short story skeptic software vancouver webcomic

Categories

  • A. D. Miller
  • A. S. Byatt
  • Adam Foulds
  • Adam Rifkin
  • Aeschylus
  • Alan Moore
  • Albert Camus
  • Alfred Lansing
  • Alison Moore
  • Amitav Ghosh
  • Andrea Levy
  • Andy Kessler
  • Angie Abdou
  • Annabel Lyon
  • Anosh Irani
  • Anton Chekhov
  • Apuleius
  • Aravind Adiga
  • Aristophanes
  • Aristotle
  • Arnaldur Indridason
  • Audrey Niefenegger
  • Ayaan Hirsi Ali
  • Barry Moser
  • Bernhard Schlink
  • Bill Crider
  • Bill O'Reilly
  • Bill Pronzini
  • Book Excerpts
  • Book Reviews
  • Boris Akunin
  • Bret Easton Ellis
  • Brett Weldele
  • Brutal Death Metal
  • Bryce Courtenay
  • C J Cherryh
  • Carol Birch
  • Carol Shields
  • Cathy Malkasian
  • Cervantes
  • Charles Burns
  • Christopher Hitchens
  • Classical
  • Clifton Fadiman
  • Clive Cussler
  • Computer Woes
  • Cormac McCarthy
  • Craigslist
  • D. H. Lawrence
  • Daily Dancer
  • Damon Galgut
  • Daniel Clowes
  • Daphne du Maurier
  • Dave Eggers
  • Dave Stewart
  • David G. Hartwell
  • Deborah Levy
  • Diana L Paxson
  • Dominique Lapierre
  • Doris Lessing
  • Douglas Adams
  • Dublin
  • E. M. Forster
  • Edward Gibbon
  • Elias Khoury
  • Ellen Datlow
  • Emma Donoghue
  • Erasmus
  • Ernest Hemingway
  • Ernest Henry Shackleton
  • Esi Edugyan
  • Frederick Forsyth
  • Funny
  • Gabriel Garcia Marquez
  • Garth Ennis
  • Gavin J. Grant
  • Gentoo
  • Geopolitics
  • George Friedman
  • George MacDonald Fraser
  • George Orwell
  • Gerald Green
  • Gianrico Carofiglio
  • Glen David Gold
  • Graham Greene
  • Graphic Novel
  • Gregory David Roberts
  • Gustave Flaubert
  • H H Munro
  • H M Naqvi
  • H. F. Saint
  • Harlan Ellison
  • Harry Harrison
  • Haruki Murakami
  • Henning Mankell
  • Henry David Thoreau
  • Herbert Wise
  • Hermann Hesse
  • Herodotus
  • Honoré de Balzac
  • Howard Jacobson
  • Ian McEwan
  • Ian Rankin
  • Ian Thomas
  • Inebriated
  • Irving Stone
  • Isaac Asimov
  • J. M. Coetzee
  • Jack London
  • Jack McDevitt
  • James E Parker Jr.
  • James Joyce
  • Jane Harris
  • Jane Smiley
  • Jane Urquhart
  • Jean-Benoit Nadeau
  • Jeet Thayil
  • Jeffery Deaver
  • Jerome K Jerome
  • Jim Butcher
  • Jim Lacey
  • Jim Mortimore
  • Jimmy McGovern
  • Jo Nesbo
  • Joe R Lansdale
  • Joel Spolsky
  • John Buchan
  • John Bunyan
  • John Kerschbaum
  • John S. Major
  • John Steinbeck
  • Jon Meacham
  • Jose Saramago
  • Julie Barlow
  • Karin Fossum
  • Karl Doenitz
  • Kathryn Cramer
  • Kazuo Ishiguro
  • Kelly Link
  • Ken Follett
  • Larry Collins
  • Lawrence Block
  • Leilah Nadir
  • Les Toil
  • Linda Landrigan
  • Linda Medley
  • Linguistics
  • Linux
  • M G Vassanji
  • Maha Gargash
  • Margaret Atwood
  • Margaret George
  • Marion Zimmer Bradley
  • Martin Cruz Smith
  • Martin Dugard
  • Martin Myers
  • Max Rodenbeck
  • Menander
  • Michael Pollan
  • Mike Ashley
  • Milton
  • Music
  • My Software
  • Nathan Fox
  • Nathanael West
  • Neal Stephenson
  • Neil Gaiman
  • New York Times Crossword
  • Noam Chomsky
  • One-Armed Bandits
  • P F Kluge
  • Pascal Mercier
  • Patrick Dewitt
  • Pauline Gedge
  • Pete Hamill
  • Peter Carey
  • Peter L. Bergen
  • Peter Lovesey
  • Peter Tremayne
  • Philip Roth
  • Phyllis Cerf Wagner
  • Plutarch
  • Programming
  • Pseudoscience
  • Ray Bradbury
  • Ray Wiss
  • Real Estate
  • Religion
  • Republican
  • Rhea Tregebov
  • Richard Tomlinson
  • Rick Geary
  • Robert Bloch
  • Robert Grudin
  • Robert Heinlein
  • Robert Sawyer
  • Robert Venditti
  • Robert Weaver
  • Robertson Davies
  • Rohinton Mistry
  • Rosemary Herbert
  • Ruth L. Ozeki
  • Saki
  • Salman Rushdie
  • Sarah Waters
  • Saros Cowasjee
  • Septoplasty
  • Sherman Alexie
  • Short Story
  • Simon Mawer
  • Sophocles
  • Stephen Clarke
  • Stephen Crane
  • Stephen Fry
  • Steve Toltz
  • Steven Pinker
  • Susan Kandel
  • Susan Wittig Albert
  • Television
  • Terry Pratchett
  • Text Processing
  • The usual
  • Thesaurus Rex
  • Thomas Hardy
  • Thomas King
  • Thucydides
  • Tim Parks
  • Tom Reiss
  • Tom Robbins
  • Tony Hillerman
  • Travel
  • Twan Eng Tan
  • Umberto Eco
  • UploadToFTP
  • Ursula Hegi
  • Vancouver
  • Vancouver 2010 Winter Olympics
  • Victor Hugo
  • Vikram Chandra
  • Virgil
  • Virginia Woolf
  • William Deverell
  • William S. Burroughs
  • Zdzisław Beksiński

Blogroll

  • Bart’s Blog
  • Berlin Photo Blog
  • Celtic Frost
  • Dev Environment
  • Geoff's Mobile Video Blog
  • Get Juand
  • It’s Curtis for you
  • Nance in France
  • New York Times Crossword Blog
  • Phil's Blog
  • That's Some Catch
  • The Zengerarium

Books

  • Arthur C. Clarke
  • Carl Sagan
  • Harlan Ellison
  • Harry Harrison
  • Harry Turtledove
  • Isaac Asimov
  • Kurt Vonnegut
  • Robert Heinlein
  • Rudyard Kipling
  • The Brothers Strugatsky

Maser

  • Are you skeptical?
  • End of Media
  • Feed Your Mind
  • Gentoo
  • Michael Shermer strikes again
  • Tanenbaum
  • That's Some Catch
  • Those Pinko Commie Liberal Tree Huggers
  • Trips

Music

  • Cryptopsy
  • Death
  • Destruction
  • Immolation
  • Kataklysm
  • Morbid Angel
  • Muspellheim
  • My Audioscrobbler page
  • Nevermore
  • Nile
  • Slayer
  • Sodom
  • Vader

Archives

  • April 2013
  • January 2013
  • December 2012
  • November 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005

Kalends

May 2013
M T W T F S S
« Apr    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Recent Posts

  • Residential Building Permits
  • Vancouver Housing Bubble
  • More Books
  • Irish Books
  • 160 days in Europe
  • Oslo
  • Zurich
  • Pickpocketed in Paris, Robbed in Rome
  • Barcelona
  • Dublin Oktoberfest

Visitors

Recent Entries

  • Residential Building Permits
  • Vancouver Housing Bubble
  • More Books
  • Irish Books
  • 160 days in Europe
  • Oslo
  • Zurich
  • Pickpocketed in Paris, Robbed in Rome
  • Barcelona
  • Dublin Oktoberfest

Recent Comments

  • Brad Murphy in Why the Salvation Army sucks
  • dro79 in Why the Salvation Army sucks
  • Mark in Why the Salvation Army sucks
  • Mark Schwendemann in Why the Salvation Army sucks
  • Mark Schwendemann in Why the Salvation Army sucks
  • Dupedbythesallyanne in Why the Salvation Army sucks
  • Mark Schwendemann in Why the Salvation Army sucks
  • jonty in Why the Salvation Army sucks
  • putapon in Why the Salvation Army sucks
  • Al in Why the Salvation Army sucks
  • Random Selection of Posts

    • Clinical Anger Scale
    • An atheist’s religion?
    • Shades of Alfred Nobel
    • New York Times Crossword Answers – Sep 03, 2008
    • The E. Coli Tour of 2011
    • Atrocious Puns
    • Avast, Arachne!
© 2008 The Inferno is proudly powered by WordPress
Theme designed by Roam2Rome