Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BSD sed; error "unescaped newline inside substitute pattern" when run from script

I am attempting to use (BSD) sed to modify my /etc/gettytab. The goal is to modify this entry:

P|Pc|Pc console:\
        :ht:np:sp#9600:

to this entry:

P|Pc|Pc console:\
        :ht:np:sp#115200:\
        :cl=\E[H\E[2J:

If I issue the command below (it's on two lines), it works perfectly.

# sed -in ' /P|Pc|Pc console/,/^#/ s/9600:/115200:\\\\  
        :cl=\E[H\E[2J:/' /etc/gettytab

However, if I use the exact same command (literally copy/paste it) in a script, I get an error message:

sed: 1: " /P|Pc|Pc console/,/^#/ ...": unescaped newline inside substitute pattern

Searching, I found this post: unescaped newline inside substitute pattern which talks about the trailing /, but I have that in my pattern.

If anyone can assist with what I am doing wrong, I would greatly appreciate it.

like image 676
Allan Avatar asked Dec 16 '25 18:12

Allan


1 Answers

Within your script, you escape newlines with a \, and you escape the \ that you're embedding into your output so that it will be interpreted literally. If my math is right, that comes to THREE, not four backslashes.

$ cat i
P|Pc|Pc console:\
        :ht:np:sp#9600:
$ cat i.sh
#!/bin/sh

#                                  ┏━━━ escapes the next character,
#                                  ┃┏━━ literal backslash for output,
#                                  ┃┃┏━ escapes the newline.
sed -ne '/^P|/,/^#/ s/9600:/115200:\\\
        :cl=\E[H\E[2J:/' -e p i

$ ./i.sh
P|Pc|Pc console:\
        :ht:np:sp#115200:\
        :cl=E[HE[2J:
$
like image 80
ghoti Avatar answered Dec 20 '25 02:12

ghoti