I'm having some difficulty with sed syntax.
I am reading in three numbers with spaces to the string coordinates. I don't think sed can handle the spaces. For example,
if $coordiantes = 0.00 0.00 0.00
and I do the following command:
Code:
sed -ie 's/coordinate_location/'$coordinates'/g' file
I get the following error:
sed: 1: "s/coordinate_location/0.00": unterminated substitute in regular expression
What is the proper way to make a substitution with a string that contains spaces?
Actually, your problem has nothing at all to do with sed. You're simply passing a malformed argument to sed because of faulty shell quoting.
Assuming that your statement:
if $coordiantes = 0.00 0.00 0.00
actually means:
I have assigned the shell variable named coordinates the value "0.00 0.00 0.00", like so:
Code:
coordinates="0.00 0.00 0.00"
At this point, the expression $coordinates or "$coordinates" will expand to the variable's value. The expression '$coordinates' will not, because $-expansion is suppressed inside single quotes.
Now look at the command-line you posted:
Code:
sed -ie 's/coordinate_location/'$coordinates'/g' file
If we do the expansion manually (always a useful exercise for understanding), we get this:
Code:
sed -ie 's/coordinate_location/'0.00 0.00 0.00'/g' file
Now, knowing that the shell breaks command lines at whitespace, exactly how will this be turned into parameters? Like this:
Code:
sed
-ie
s/coordinate_location/0.00
0.00
0.00/g
file
From this, it's trivial to see that the s/pat/replace/ is malformed, because it lacks the third /.
Here's a useful tip:
Write a simple program that just outputs its argv values, one per line.
Run this program to see what args are actually being passed in.
For example, if the program were called 'argyle', then this command-line:
Code:
argyle -ie 's/coordinate_location/'$coordinates'/g' file
would show output very like the breakdown given above.
BTW, awk can do substitution, too. sed may not even be needed here.