Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Batch replace strings in all files in a folder--when there are UNIX-style slashes

  1. #1
    Join Date
    Sep 2008
    Beans
    540
    Distro
    Kubuntu 12.04 Precise Pangolin

    Question Batch replace strings in all files in a folder--when there are UNIX-style slashes

    So I have a directory full of m3u files (playlists, which are essentially XML files). I moved my main directory full of music, and now need to replace all the paths in all those files. I need to change

    /home/tarahmarie/Music/

    to

    /data/Music/

    The problem is that I'm not sure how to go about it, since there are slashes in that string, and the only way I've seen to do this via CL with perl is

    Code:
    find /path/to/start/from/ -type f | xargs perl -pi -e 's/applicationX/applicationY/g'
    The problem is that I'd be doing this:

    Code:
    find /path/to/start/from/ -type f | xargs perl -pi -e 's/home/tarahmarie/data/g'
    And I don't think that works, since I've got that extra slash in the string.

    How might I modify this to work?
    Registered Linux User #479009
    Friend-->Linux; 6 and counting...

  2. #2
    Join Date
    Jan 2007
    Location
    Michigan, USA
    Beans
    1,184
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    lookup the syntax for sed and or awk, it should be very simple to setup a one liner to make this task happen.

  3. #3
    Join Date
    Sep 2008
    Beans
    540
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    Right, but I'm trying to modify that perl script, since sed and awk are bloody complicated.
    Registered Linux User #479009
    Friend-->Linux; 6 and counting...

  4. #4
    Join Date
    May 2009
    Location
    Colorado, USA
    Beans
    87
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    Escape the directory separator with a back slash.

    's/home\/tarahmarie/data/g'

    Try that.

    Edited to add:
    Like t4thfavor, my first thought was to use sed (stream editor) to do this. It has the -i option to do in-place editing. For this purpose, sed is no more complicated than using perl. Here's something to get you started with sed. This should be what comes after xargs.

    Code:
    sed -i 's/home\/tarahmarie/data/g'
    2nd edit:
    I tested the above command and it works with a file argument -- should also work fine with xargs.
    Last edited by StephenDavison; December 13th, 2010 at 05:57 AM. Reason: added code tag and tested the command
    MSI Wind U100-432US (Synaptics touchpad, Intel Wifi Link 1000, Bison webcam rev.03), Ubuntu 10.10
    System76 Ratel Ultra (Core i5-650, 4GB DDR3, 1.5 TB HDD)

  5. #5
    Join Date
    Jan 2007
    Location
    Michigan, USA
    Beans
    1,184
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    Definately intimidating until you start to understand them. Then it's just plain scary because you begin to understand them...

    Code:
    sed 's/\\home\\oldpath\\/\\home\\newPath\\/g' test.txt>> test2.txt

  6. #6
    Join Date
    May 2006
    Beans
    1,790

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    Quote Originally Posted by t4thfavor View Post
    Definately intimidating until you start to understand them. Then it's just plain scary because you begin to understand them...

    Code:
    sed 's/\\home\\oldpath\\/\\home\\newPath\\/g' test.txt>> test2.txt
    All of these support using a different separator than slash. Just use any one you want, like s#applicationX#applicationY#g

  7. #7
    Join Date
    Sep 2008
    Beans
    540
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    Quote Originally Posted by Arndt View Post
    All of these support using a different separator than slash. Just use any one you want, like s#applicationX#applicationY#g
    Can I specify that I want them written back to the same filename?
    Registered Linux User #479009
    Friend-->Linux; 6 and counting...

  8. #8
    Join Date
    Nov 2009
    Location
    The Netherlands
    Beans
    239
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    Quote Originally Posted by tarahmarie View Post
    Can I specify that I want them written back to the same filename?
    If you use sed, the -i parameter makes it work in-place, so the changes will be made in the file instead of printed to the terminal.

  9. #9
    Join Date
    May 2009
    Location
    Colorado, USA
    Beans
    87
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    Have you read all the replies? I posted that 16 hours ago.
    MSI Wind U100-432US (Synaptics touchpad, Intel Wifi Link 1000, Bison webcam rev.03), Ubuntu 10.10
    System76 Ratel Ultra (Core i5-650, 4GB DDR3, 1.5 TB HDD)

  10. #10
    Join Date
    Sep 2008
    Beans
    540
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Batch replace strings in all files in a folder--when there are UNIX-style slashes

    Here's my output when I try that:

    Code:
    /data/Music/testplay Hello! $sed -i 's/\\home/tarahmarie/Music\\/\\data/Music\\/g'
    sed: -e expression #1, char 22: unknown option to `s'
    Registered Linux User #479009
    Friend-->Linux; 6 and counting...

Page 1 of 2 12 LastLast

Tags for this Thread

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
  •