Extracting specific line of text with grep for use in conky

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.

dgz

Posted 2013-05-09T19:58:05.883

Reputation: 1

Answers

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.

slhck

Posted 2013-05-09T19:58:05.883

Reputation: 182 472

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 – 2013-05-09T21:28:27.807

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.

Ben Richards

Posted 2013-05-09T19:58:05.883

Reputation: 11 662

I'd like to know what's wrong with this answer from whomever down-voted it. – Ben Richards – 2013-05-09T21:02:57.293

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 – 2013-05-09T21:41:26.657

@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 – 2013-05-09T22:00:50.600

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 – 2013-05-10T00:19:49.990

@kine Good point, though for many tasks it would be a negligible difference in efficiency. – Ben Richards – 2013-05-10T14:58:07.480

0

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

$ echo /music/album/03-song.ogg| sed -r 's@^(.+)/.+$@\1@'
/music/album

tink

Posted 2013-05-09T19:58:05.883

Reputation: 1 419

0

quodlibet --print-playing "<~dirname>"

lazka

Posted 2013-05-09T19:58:05.883

Reputation: 101