PDA

View Full Version : [SOLVED] Using sed with slashes



The_Weaver
September 19th, 2014, 10:20 PM
I'm trying to replace a directory location with the relative location in a number of playlists ie replace "../home/keith/Music/Songs" with "..". After a bit of research I think sed could be useful.

I think the following command is what I what to use :

find /home/keith/Music/Songs/Playlists -type f -readable -writable -exec sed -i "s#../home/keith/Music/Songs#..#g"{} \;

but it fails since I'm using slashes.

Any suggestions?

Thanks

steeldriver
September 19th, 2014, 11:34 PM
You appear to be handling the slashes sensibly (i.e. using a # as the sed delimiter instead)

However you should replace the leading .. with \.\. or [.][.] to make it match literal dots - else it will match any two characters

How exactly is it failing? Can you post a minimal example?

Vaphell
September 20th, 2014, 12:28 AM
does it really start with ..? /home/user/..... looks like an absolute path and .. shouldn't make sense

nerdtron
September 20th, 2014, 01:43 AM
Have you tried encasing the sed parameters in single quotes?

Also have you tried escaping the slashes with backslashes?

CaptainMark
September 20th, 2014, 07:00 AM
Are you not able to use the dirname command for this, its been a while since I used it but it was included in a standard install

The_Weaver
September 20th, 2014, 08:31 AM
I've removed the leading .. as it was an absolyet path and not required, and relasied I was missing a space before the {}. So the command now is:

find /home/keith/Music/Songs/Playlists -type f -readable -writable -exec sed -i "s#/home/keith/Music/Songs#..#g" {} \;

and it worked.

Many thanks

steeldriver
September 20th, 2014, 01:48 PM
Ah good - for the record, it's a lot easier to spot things like missing space if you post commands between [CODE] markup tags