LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed error (https://www.linuxquestions.org/questions/programming-9/sed-error-520799/)

ShaqDiesel 01-19-2007 03:04 AM

sed error
 
hello, in a ksh script I have
sed "s/\/\([a-ZA-z]*\)/[[$str\/\1]]/g"
where $str was a string stored earlier in this kornshell script.

For example, the script stored "cheese" in $str. Then it would substitute /Burger with [[cheese/Burger]]. However I get a "sed statement not closed" error. What's wrong with my syntax? Thanks in advance.

druuna 01-19-2007 03:14 AM

Hi,

Maybe you didn't give all the info needed, 'cause the following seems to work:
Code:

#!/bin/ksh

str="cheese"

sed "s/\/\([a-ZA-z]*\)/[[$str\/\1]]/g" infile

Example run:
Code:

$ cat infile
/burger

$ ./tets
[[cheese/burger]]

-----------------------------------
I tested this on SUN hardware with solaris 9. In general solaris is less forgiving then linux, in this case it is the opposite. The [a-ZA-z] part will not work with linux, it does with solaris. Sorry for the confusion.

matthewg42 01-19-2007 03:45 AM

The problem is [a-ZA-z], which should be [a-zA-Z]. Think of ranges of character as their ASCII values. Saying [a-z] is like saying (pseudocode):
Code:

if ( char >= ascii('a') && char <= ascii('z') ...
Plugging in the ASCII values, saying [a-Z] would work out as:
Code:

if ( char >= 97 && char <= 90 ) ...
which you should be able to see doesn't make sense - it can never be true. The regexp engine checks for apparent errors like this, and gives an error when it finds one.

ShaqDiesel 01-19-2007 12:12 PM

thanks, I can't believe I didn't catch that. My problem now is that it doesn't interpret $str as a variable, but a literal; my result is [[$str/burger]] instead of [[cheese/burger]]; how do I fix this?

druuna 01-19-2007 12:18 PM

Hi,

Which quotes are you using around the sed statement: single or double? It should be double quotes, as shown in the example in my previous post.

If that is not the case, could you post the code you are using so we can have a look?


All times are GMT -5. The time now is 12:50 PM.