You are not logged in.

#1 2008-01-14 19:22:44

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

[SOLVED] Some help with sed, or something like it

I'm writing a little script in bash that displays my current MPD music database in ratmen.
Ratmen is a very simple menu for Ratpoison, that works in this way: it accepts a menu item and a command associated with it, and when you want to use multiple items, you have to set up a delimiter between the item and its command, in this way:

 ratmen -d?? "item??command" "item??command" "item??command"

As you can see in the example, the separator is "??".

Now, what I have done is creating a script that dynamically generates the list of my album folders and creates a command to play them, so that as a final result I have (more or less):

 ratmen -d?? "Album Title??mpc ls Album Title | mpc play" "Album Title??mpc ls Album Title | mpc play"

Trivial, isn't it? tongue

So far so good. I have made extensive use of sed, since I don't know much about programming. Now my problem is with single quotes in albums, it really messes up everything, and all the albums with a single quote inside do not work.
So, this is the script:

#!/bin/bash

mpc ls | sed -e "s/\(.*\)/\1??mpc clear; mpc ls \'\1\' | mpc add; mpc play/" | sed -e 's/\(.*\)/"  \1  " /' | sed ':a;N;$!ba;s/\n//g' | sed 's/^/ratmen -d?? --title \"MPD Database\" --foreground beige --background gray33 /' > ./ratpoison-scripts/mpcdump;
chmod +x "./ratpoison-scripts/mpcdump";
exec "./ratmen-scripts/mpcdump"

Whenever I run it, it creates a new file, called mpcdump and executes it. This file contains the actual ratmen command with arguments, like I have indicated above. The main problem is that I already use single quotes to quote the album titles, and double quotes to separate the ratmen arguments, like this:

 ratmen -d?? "Album title??mpc play; mpc ls 'Album Title' | mpc play"

I have found a way of adding a single quote using escapes (using '\''), but it messes up my titles in the menu, since it doesn't get escaped in them. So I need a way to only edit the second part of the argument, namely only the title that appears in single quotes.

I hope this is clear enough, and I'm posting a screenshot to make it even clearer:

shot-140108595.png.xs.jpg

I have not replaced the single quotes with '\'' yet, since it's too messed up, and I prefer not to be able to select albums than having it all messed up tongue

So, do you think I can do this?

Thanks smile

Last edited by finferflu (2008-01-15 00:33:59)


Have you Syued today?
Free music for free people! | Earthlings

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery

Offline

#2 2008-01-14 20:07:12

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] Some help with sed, or something like it

EDIT - see next post

Last edited by Cerebral (2008-01-14 20:15:42)

Offline

#3 2008-01-14 20:15:28

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] Some help with sed, or something like it

Tested - this should work for you

#!/bin/bash

rm ./ratpoison-scripts/mpcdump;

echo "ratmen -d?? --title \"MPD Database\" --foreground beige --background gray33 \\" >> ./ratpoison-scripts/mpcdump

mpc ls | sed "

# Copy title to hold buffer
h

# Escape single quotes
s#'#\\\\'#g

# Add ending
s#\(.*\)#\1' | mpc add; mpc play  \" \\\\#

# Swap hold and pattern buffers
x

# Add beginning
s#\(.*\)#\t\"  \1??mpc clear; mpc ls '#

# Get the end
G

# Remove any funny newlines
s#\n##g

" >> ./ratpoison-scripts/mpcdump;

chmod +x "./ratpoison-scripts/mpcdump";
exec "./ratmen-scripts/mpcdump"

Offline

#4 2008-01-14 20:40:19

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

Re: [SOLVED] Some help with sed, or something like it

Thanks a lot for your effort, Cerebral smile

Unfortunately it doesn't work sad

this is the not so unusal error I get:

sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file

And this is the command it generates when I select an album (there is an option in ratmen to only show the command):

mpc clear; mpc ls '2 Orchestra - Mercutio\'s Dead' | mpc add; mpc play

So it seems unhappy with \'. I have tried to only run the actual generated command and this is what I get at the prompt:

~ > mpc ls '2 Orchestra - Mercutio\'s Dead' | mpc add; mpc play
>

so it really seems it doesn't escape it.


Have you Syued today?
Free music for free people! | Earthlings

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery

Offline

#5 2008-01-14 20:50:40

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] Some help with sed, or something like it

You get that error when you click on the items in your list, you mean?

Try replacing

# Escape single quotes
s#'#\\\\'#g

in the script I gave with

# Escape single quotes
s#'#\\\\\\\\'#g

(... so ugly, but it adds another \)

Offline

#6 2008-01-14 21:03:55

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

Re: [SOLVED] Some help with sed, or something like it

Yes, I get it when I click on them. And as I have shown you, if you type the command generated by your script on the command line, it doesn't get executed.

I have added the extra backslashes, but I get the same error, I think the quotes don't get escaped despite the backslash...

Thanks for coming down into this quotes nightmare with me tongue


Have you Syued today?
Free music for free people! | Earthlings

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery

Offline

#7 2008-01-14 21:15:55

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] Some help with sed, or something like it

Oh... yeah, silly me.  Single quotes are screwing you over here, since the stuff between them is considered literal.  Try this:

edit - won't work. BAH.  Dumb quotes.

Last edited by Cerebral (2008-01-14 21:17:11)

Offline

#8 2008-01-14 21:16:53

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] Some help with sed, or something like it

ohwait... that won't work either.  give me a sec

Offline

#9 2008-01-14 21:19:13

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] Some help with sed, or something like it

#!/bin/bash

rm ./ratpoison-scripts/mpcdump;

echo "ratmen -d?? --title \"MPD Database\" --foreground beige --background gray33 \\" >> ./ratpoison-scripts/mpcdump

mpc ls | sed "

# Copy title to hold buffer
h

# Escape single quotes
s#'#'\"'\"'#g

# Add ending
s#\(.*\)#\1' | mpc add; mpc play  \" \\\\#

# Swap hold and pattern buffers
x

# Add beginning
s#\(.*\)#\t\"  \1??mpc clear; mpc ls '#

# Get the end
G

# Remove any funny newlines
s#\n##g

" >> ./ratpoison-scripts/mpcdump;

chmod +x "./ratpoison-scripts/mpcdump";
exec "./ratpoison-scripts/mpcdump"

Test?

Offline

#10 2008-01-14 21:38:12

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

Re: [SOLVED] Some help with sed, or something like it

Yeah, welcome to my nightmare tongue

It's still not working sad

> ./mpclist
~/ratpoison-scripts/mpcdump: line 99: unexpected EOF while looking for matching `"'
~/ratpoison-scripts/mpcdump: line 100: syntax error: unexpected end of file

Edit:

Ok, I opened ./ratpoison-scripts/mpcdump, and I saw how one of the lines was rendered:

"  2 Orchestra - Mercutio's Dead??mpc clear; mpc ls '2 Orchestra - Mercutio'"'"'s Dead' | mpc add; mpc play  "

I guess it doesn't like that either...

Last edited by finferflu (2008-01-14 21:43:34)


Have you Syued today?
Free music for free people! | Earthlings

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery

Offline

#11 2008-01-14 21:48:44

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] Some help with sed, or something like it

DOH.  One more time.

#!/bin/bash

rm ./ratpoison-scripts/mpcdump;

echo "ratmen -d?? --title \"MPD Database\" --foreground beige --background gray33 \\" >> ./ratpoison-scripts/mpcdump

mpc ls | sed "

# Copy title to hold buffer
h

# Escape single quotes
s#'#'\\\\\"'\\\\\"'#g

# Add ending
s#\(.*\)#\1' | mpc add; mpc play  \" \\\\#

# Swap hold and pattern buffers
x

# Add beginning
s#\(.*\)#\t\"  \1??mpc clear; mpc ls '#

# Get the end
G

# Remove any funny newlines
s#\n##g

" >> ./ratpoison-scripts/mpcdump;

chmod +x "./ratpoison-scripts/mpcdump";
exec "./ratpoison-scripts/mpcdump"

Offline

#12 2008-01-14 21:53:45

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

Re: [SOLVED] Some help with sed, or something like it

Yeah! you did it! big_smile

I wish we had a triple quote sometims tongue
Anyway I have learnt the hold buffer technique thanks to your help.

Thanks a lot! smile


Have you Syued today?
Free music for free people! | Earthlings

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery

Offline

Board footer

Powered by FluxBB