sed error "unescaped newline inside substitute pattern"

David Banning david+dated+1237386563.c8ac2e at skytracker.ca
Fri Mar 13 07:55:35 PDT 2009


Nikos Vassiliadis wrote:
> David Banning wrote:
>> Here is the php line that gives the error;
>>
>> cat start_text | sed "s/--maintext--/$test/" > endtext
>>
>> give error;
>>
>> sed: 1: "s/--maintext--/ Comment ...": unescaped newline inside 
>> substitute pattern
>>
>> where $test contains customer input from a website form
>>
>> There is something about the content of the text within the variable 
>> $test that is causing the error.
>>
>> Any pointers would be helpful.
>
> You cannot use unescaped newlines in the replacement string:
> # sed 's/foo/foo<<<Press enter here>>>
> > bar/'
> sed: 1: "s/foo/foo
> bar/": unescaped newline inside substitute pattern
>
> You have to precede each newline character with a backslash:
> # sed 's/foo/foo\<<<Press enter here>>>
> > bar/'
> foo
> foo
> bar
>
> The examples above are made using the bourne shell. The
> above doesn't work in csh.
>
> It's the documented behavior:
>  2.   The escape sequence \n matches a newline character embedded in the
>  pattern space.  You cannot, however, use a literal newline character
>  in an address or in the substitute command.
>
>  ...
>
>  A line can be split by substituting a newline character into it.
>  To specify a newline character in the replacement string, precede
>  it with a backslash.
>
> I am sure there is some good reason behind this...
>
> OT, you should not pass parameters to shell scripts
> using double quotes, since the shell will evaluate
> possible variable values:
> # echo "$OSTYPE"
> FreeBSD
> # echo '$OSTYPE'
> $OSTYPE
>
> I really doubt you want the first behavior, that is, variable
> evaluation for strings coming from a web server.
>
> HTH, Nikos
The problem that I am having is that the input is from a web form - so I 
have to deal with it - I have to somehow modify it so it can pass sed.


More information about the freebsd-questions mailing list