#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Replacing a string using sed


    I am attempting to replace all instances of the string "sequentialInsert" with the string "orderedInsertion" for a particular file. I must accomplish this in one line of code. Additionally, the input file and output file must be in two different directories.

    This is what I have tried:

    sed "s/sequentialInsert/orderedInsertion" path1 < path2

    Where path1 is the path of the input file and path2 is the path of the output file (which does not exist prior to the execution of this command, although all relevant directories in path2 do exist).

    When I issue this command I get the following error:

    sed: -e expression #1, char 35: unterminated 's' command

    I do not understand what I am doing wrong. I would appreciate an explanation and if someone would point me in the right direction.

    Thanks.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    I found the solution, although I don't totally understand it.

    The correct command is as follows:

    sed "s/sequentialInsert/orderedInsertion/" path1 > path2

    The difference here is the third "/" at the end. According to something I read, the error that I received is common if you have too few delimiters, and in this case the "/" character is acting as a delimiter.
  4. #3
  5. No Profile Picture
    Still Learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Montreal, Canada
    Posts
    55
    Rep Power
    40
    The triple delimiter is a very common pattern in sed, awk, perl and in general in many Perl Compatible Regular Expressions.

    When you use a substitution in a script rather than on the command line there are no quotes around the substitute commnad, so the last delimiter defines the end of the second string.

    Originally Posted by Herpopotamus
    I found the solution, although I don't totally understand it.

    The correct command is as follows:

    sed "s/sequentialInsert/orderedInsertion/" path1 > path2

    The difference here is the third "/" at the end. According to something I read, the error that I received is common if you have too few delimiters, and in this case the "/" character is acting as a delimiter.

IMN logo majestic logo threadwatch logo seochat tools logo