Results 1 to 7 of 7

Thread: Using sed with slashes

  1. #1
    Join Date
    Aug 2009
    Beans
    22

    Using sed with slashes

    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

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Using sed with slashes

    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?

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Using sed with slashes

    does it really start with ..? /home/user/..... looks like an absolute path and .. shouldn't make sense
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Aug 2009
    Location
    Makati City, Philippines
    Beans
    2,269
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Using sed with slashes

    Have you tried encasing the sed parameters in single quotes?

    Also have you tried escaping the slashes with backslashes?

  5. #5
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    1,524
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Using sed with slashes

    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
    Catch me on Freenode - imark

  6. #6
    Join Date
    Aug 2009
    Beans
    22

    Re: Using sed with slashes

    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

  7. #7
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Using sed with slashes

    Ah good - for the record, it's a lot easier to spot things like missing space if you post commands between [CODE] markup tags

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •