-1

In my project in .sh file I wrote this command:

var1=`sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g;s/^/"/g;s/$/"/g' ${var}`
sed -i "s/%PQR%/$var1/g" file2

Here var is the variable which stores location of the file that I want to update. I want double quotes to be added at the start and end of file. Also I want all the new lines to be replaced with \n. example:

To err is to human.
This is life.

output:

"To err is to human.\nThis is life."

Then I want %PQR% in file2 to be replaced with var1.

But on running pipeline, the get the error on the second command as described in the title:

sed: -e expression#1 char 48: unterminated `s' command
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • Does it work if you use " instead of " and also quote the var, like this: "$var" ?

    I guess you could try each sed expression individually, to see if they all work.

    – Alexander Oct 05 '21 at 09:51
  • Welcome, check at this about the use of backticks. – schrodingerscatcuriosity Oct 05 '21 at 09:53
  • You've got only one backtick on that first line. Copy typo? (Please fix it.) What happens if you run the two commands separately - which one generates the error message? What's the value of $var1 after setting it? Prefix the second sed command with echo. Does what you see look like a valid sed command? – Chris Davies Oct 05 '21 at 09:54
  • That is a typing error: I have used backtick properly in my code – Harsimran Kaur Oct 05 '21 at 10:05
  • I tried executing both the commands separately. Command one runs fine. There seems to be an issue with command two. Can anyone please help with that. – Harsimran Kaur Oct 05 '21 at 10:43
  • "unterminated 's' command" usually means you're missing the one of the separators (the slashes), or you have a newline in the sed code that would prematurely terminate the command. – ilkkachu Oct 05 '21 at 10:46
  • But we can't know what the issue with sed is, since there's an unterminated command substitution there, so even those shell commands won't run won't run. You might want to use e.g. shellcheck.net to check the syntax. Then, you show one error message, but don't tell which sed command gives it. Reduce the problem by removing the parts that are not a problem, and then show a complete, working, but minimal piece of code that shows the problem, along with any values you use. Or even enable set -x and show the output too, so we can see what actually runs. – ilkkachu Oct 05 '21 at 10:47
  • "I tried executing both the commands separately. Command one runs fine. There seems to be an issue with command two." Please put this into your question. Comments are not for adding information as it's too easy to miss something. – Chris Davies Oct 05 '21 at 12:20

1 Answers1

1

To encode the contents of a file as one json string, use

json=$(jq --slurp -R < "$file")

Or:

json=$(jq --slurp -R 'rtrimstr("\n")' < "$file")

To trim the newline character that delimits the last line.

And to replace some placeholder with arbitrary data, it's much easier with perl (the -i option that some sed implementations support was actually inspired by perl).

PLACEHOLDER='%PQR%' REPLACEMENT="$json" perl -pi -e '
  s/\Q$ENV{PLACEHOLDER}\E/$ENV{REPLACEMENT}/g' -- "$other_file"

It's possible with sed, though you'd need to escape some characters.