0

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.

4 Answers 4

2

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.

1
  • 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. May 9, 2013 at 21:28
0

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.

5
  • I'd like to know what's wrong with this answer from whomever down-voted it. May 9, 2013 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, 2013 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. May 9, 2013 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, 2013 at 0:19
  • @kine Good point, though for many tasks it would be a negligible difference in efficiency. May 10, 2013 at 14:58
0

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

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

You must log in to answer this question.

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