[Cialug] sed question

Pix cialug at kepibu.org
Fri Sep 23 15:38:16 UTC 2022


On 9/21/22 09:57, Todd Walton wrote:
> What am I doing wrong here? I set two variables and then try to use them in
> a sed line to update a file. But I get "unterminated s command". I'm sure
> it's some stupid shell quoting thing.
> 
>> todd:[~]> printf -v starparen "\x2A\x29"
>>
>> todd:[~]> printf -v gitrun
> '%s\n%s\n%s\x22%s\x22\n%s\x22\x24%s\x22\n%s\n\n%s' "gitlab-runner)" "
> shift" "       echo " "Running Runner" "       gitlab-runner " "{@}" "
> ;;" "     *)"
>>
>> todd:[~]> echo ${starparen}
>> *)
>>
>> todd:[~]> echo "${gitrun}"
>> gitlab-runner)
>>        shift
>>        echo "Running Runner"
>>        gitlab-runner "${@}"
>>      ;;
>>
>>      *)
>>
>> todd:[~]> sed -i.bak "s,${starparen},${gitrun},g" script.sh
>> sed: -e expression #1, char 19: unterminated 's' command
> 
> Any suggestions? Any ideas about what's going awry?
char 19 would put that at the newline, yes?

  s,*),gitlab-runner)<newline>
                     ^

Which suggests maybe sed no likey the newline.  A bit of rummaging
around `info sed` says commands are terminated by newlines, which would
seem to confirm that:

> Commands within a SCRIPT or SCRIPT-FILE can be separated by semicolons
(';') or newlines (ASCII 10).

Fortunately, documentation for the `s` command has some suggestions for
how to deal with that:

> To include a literal '\', '&', or newline in the final replacement, be
sure to precede the desired '\', '&', or newline in the REPLACEMENT with
a '\'.


-pix


More information about the Cialug mailing list