Need Help With Script - Ubuntu

This is a discussion on Need Help With Script - Ubuntu ; I am looking for a script that traverses the directory tree starting with the current directory and replaces string1 (an email address) with string2 (another email address). Sounds easy to do with sed but I can't seem to get my ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Need Help With Script

  1. Need Help With Script

    I am looking for a script that traverses the directory tree starting
    with the current directory and replaces string1 (an email address) with
    string2 (another email address).

    Sounds easy to do with sed but I can't seem to get my brain around the
    details. I think I'm getting too old for Unix!!!

    Thanks.

  2. Re: Need Help With Script


    "Juan Wei" wrote in message
    news:fnnihl$8pc$1@registered.motzarella.org...
    :I am looking for a script that traverses the directory tree starting
    : with the current directory and replaces string1 (an email address) with
    : string2 (another email address).
    :
    : Sounds easy to do with sed but I can't seem to get my brain around the
    : details. I think I'm getting too old for Unix!!!
    :
    : Thanks.

    Open the package manager and install kfilereplace?



  3. Re: Need Help With Script

    Cork Soaker has written on 1/29/2008 11:49 AM:
    > "Juan Wei" wrote in message
    > news:fnnihl$8pc$1@registered.motzarella.org...
    > :I am looking for a script that traverses the directory tree starting
    > : with the current directory and replaces string1 (an email address) with
    > : string2 (another email address).
    > :
    > : Sounds easy to do with sed but I can't seem to get my brain around the
    > : details. I think I'm getting too old for Unix!!!
    > :
    > : Thanks.
    >
    > Open the package manager and install kfilereplace?


    Unfortunately, the directory hierarchy I need to edit is on my ISP's
    computer.

  4. Re: Need Help With Script

    * Juan Wei :
    > Cork Soaker has written on 1/29/2008 11:49 AM:
    >> "Juan Wei" wrote in message
    >> news:fnnihl$8pc$1@registered.motzarella.org...
    >> :I am looking for a script that traverses the directory tree starting
    >> : with the current directory and replaces string1 (an email address) with
    >> : string2 (another email address).
    >> :
    >> : Sounds easy to do with sed but I can't seem to get my brain around the
    >> : details. I think I'm getting too old for Unix!!!
    >> :
    >> : Thanks.
    >>
    >> Open the package manager and install kfilereplace?

    >
    > Unfortunately, the directory hierarchy I need to edit is on my ISP's
    > computer.


    Then is this really an Ubuntu question? Seems more of a general Unix
    shell scripting question. You may find comp.unix.shell more helpful.
    However, I can advise looking into find and sed or Perl if it's
    available on your ISP's server. Those should be able to do what you've
    asked how to do. It's impossible to be more specific than that without
    knowing more about what you'd like to do.

    The following is an example of using find and Perl to do something like
    you've described:

    find . -type f -exec perl -pi.bak -e 's/string1/string2/g' {} \;

    "string1" would be replaced by "string2" in all files, and a backup copy
    with an extension of '.bak' would be made of each file.

    --
    James Michael Fultz
    Remove this part when replying ^^^^^^^^

  5. Re: Need Help With Script

    On Tue, 29 Jan 2008 10:59:48 -0500, Juan Wei wrote:

    > I am looking for a script that traverses the directory tree starting
    > with the current directory and replaces string1 (an email address) with
    > string2 (another email address).
    >
    > Sounds easy to do with sed but I can't seem to get my brain around the
    > details. I think I'm getting too old for Unix!!!
    >
    > Thanks.


    You don't need a script. Just "find" and "sed". Find does a recursive
    search for filenames and sed is an editor that's controlled by
    command-line arguments. This is off the top of my head so try it in a
    test directory first, but I'm pretty sure this is how the command
    would look:

    find startpath -type f -name 'filespec' -exec sed -i 's/string/replacement/g' '{}' ';'

    "startpath" is where you want "find" to start searching. Omit this or
    replace it with a single period to start in the current directory.

    "-type f" means the command is to be executed only for files. This'll
    keep you from trying to execute a file-oriented command on a directory
    name that happens to match your filespec. For directory-oriented
    operations, you'd use "-type d".

    "-name 'filespec'" says to seek out files matching the given
    filespec. It should be enclosed in quotes if you want to use wildcards
    ("*" and "?"). You can leave "-name 'filespec'" out if you want to
    execute the command for all files.

    "-exec" means to execute the following commands for any matching
    files. It's followed by the command to be executed, enclosing any
    strings or special characters in single quotes to keep them from being
    interpreted by the shell. '{}' gets replaced by the matched filename
    and ';' indicates the end of the execute command. Note that they both
    have to be in single quotes.

    "sed" is the command you want to execute for each file.

    "-i" means to edit the file in-place. Without this, the original file
    remains unchanged and the altered text is sent to stdout for
    redirection. In Linux, changing a file while redirecting its changed
    version back to the same filename usually trashes the file because the
    redirection deletes the original before it's been fully read. So, play
    it safe and use -i instead.

    "-i.bak" can be used instead of "-i" if you want a backup file made
    before modifying the file. In this example, ".bak" will be appended to
    the original filename to create the backup name.

    "s/string/replacement/" says to search the specified file (which find
    substitutes for '{}') for the FIRST occurrence of "string" and replace
    it with "replacement". If you append a g to that, it tells sed to
    replace *all* occurrences in the file: "s/string/replacement/g". If
    your string or its replacement contain a "/", you can use having to
    use messy escape characters by specifying a different delimiter. Sed
    uses whatever follows the initial "s" as its delimiter. For example:
    sed -i.bak s#string#replacement#g test.txt

    '{}' is replaced by "find" with the matched filename.

    Finally, ';' marks the end of the command. This is REQUIRED. Some
    people use a backslash instead of the single quotes: \;


    Find can mess up a lot of files fast if you feed it a bad
    command. It's a good idea to try your command on a test directory
    before executing across a directory tree containing important files!

  6. Re: Need Help With Script

    Spinner has written on 1/29/2008 1:48 PM:
    > You don't need a script. Just "find" and "sed". Find does a recursive
    > search for filenames and sed is an editor that's controlled by
    > command-line arguments. This is off the top of my head so try it in a
    > test directory first, but I'm pretty sure this is how the command
    > would look:
    >
    > find startpath -type f -name 'filespec' -exec sed -i 's/string/replacement/g' '{}' ';'
    >


    Ah, that brings back memories!! :-) Thanks.

  7. Re: Need Help With Script

    Spinner has written on 1/29/2008 1:48 PM:
    > find startpath -type f -name 'filespec' -exec sed -i 's/string/replacement/g' '{}' ';'
    >


    Late note: it worked fine when I unescaped the curly brackets. Thanks
    again, I knew it was easy!! :-)

  8. Re: Need Help With Script

    On Tue, 29 Jan 2008 18:32:58 -0500, Juan Wei wrote:

    > Spinner has written on 1/29/2008 1:48 PM:
    >> find startpath -type f -name 'filespec' -exec sed -i
    >> 's/string/replacement/g' '{}' ';'
    >>
    >>

    > Late note: it worked fine when I unescaped the curly brackets. Thanks
    > again, I knew it was easy!! :-)


    Oh, sorry. Ubuntu makes everything so easy I'm forgetting the "old
    ways".

    You need to escape the braces when you use find in a sh script, but at the
    command line it depends on your shell.

+ Reply to Thread