November 29th, 2012, 02:58 PM
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.
November 29th, 2012, 03:26 PM
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.
December 12th, 2012, 07:04 AM
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