Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to extract the directory of the current playing song in Quodlibet for use in attempting to display album artwork in conky.

In QL's current file ~/.quodlibet/current there is a line that gives you the full path of the currently playing song, but I only need the directory. Is there a way I can ignore the last part of the path with grep?

For example if I had a file called /music/album/03-song.ogg, I'd only want grep to output everything before the 0 (or 1/2/3/4 for longer albums).

I've no idea if I can use this for what I need it for, but it would still be useful to know for future reference.

share|improve this question

If the current file contains a valid path to a file, simply get its directory name:

dirname $(< ~/.quodlibet/current)

The $(< …) will read a file and substitute its contents as if it was cat.

You don't actually want to parse the file or use any regular expressions. This is likely to break if the directory name contains a "0", for example. The only reliable source of information for determining the directory is the last slash, and dirname handles just that anyway.

share|improve this answer
This is a more elegant solution, but while regular expressions can easily break (they aren't too user-friendly), you can parse directories effectively with them. – Ben Richards May 9 '13 at 21:28

You can do this by piping your grep output to sed and using regular expressions to extract the info you want.

You can read up on sed and its regular expression syntax from its manual page.

share|improve this answer
I'd like to know what's wrong with this answer from whomever down-voted it. – Ben Richards May 9 '13 at 21:02
It wasn't me who downvoted you, but if you're going to use sed anyway, why bother with a grep in the first place? I'd say it was unnecessary effort. – tink May 9 '13 at 21:41
@tink Putting it in multiple stages makes the regular expressions you have to write simpler, because you're breaking the problem down into simpler parts. You can rewrite it with a single regex, but that can easily make it very unwieldy. I've done both, and I prefer piping. – Ben Richards May 9 '13 at 22:00
You can write it in multiple stages without involving grep. grep 'whatever' ~/.quodlibet/current | sed 'whatever2' is equivalent to sed '/whatever/!d; whatever2' ~/.quodlibet/current. You can string any number of expressions within sed and it will be aesthetically equivalent to piping, but (depending on what you're doing) more efficient. – kine May 10 '13 at 0:19
@kine Good point, though for many tasks it would be a negligible difference in efficiency. – Ben Richards May 10 '13 at 14:58

Just because I like sed :) ... love @slhck's brilliant solution!

$ echo /music/album/03-song.ogg| sed -r 's@^(.+)/.+$@\1@'
share|improve this answer
quodlibet --print-playing "<~dirname>"
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .