I am using Tecplot to process wind tunnel data, but the input for Tecplot requires a specific format for variable specification; each variable is bracketed by double quotes "Variable Name". The problem is getting the double quotes isn't easy. I found in one post that printf ' "%s" ' will produce this effect. However, printf is rather finicky about what it will take as input. I've been unable to pipe the variables from my data file using a sed before the printf but, I found another post that noted using printf ' "%s" ' $(sed ...). This construct worked but is only half of the solution to my problem. I now want to use the output of this printf in a substitution with sed to swap out a placeholder (XYZXYZXYZ) with the contents of the printf ' "%s" ' $(sed ...). All I get is an unterminated 's:

sed s/XYZXYZXYZ/` printf ' "%s" ' $(sed -n 1,265p Run-0020) `/ ../../wt/wt-layout_A.dat
sed: -e expression #1, char 12: unterminated `s' command

How do I get the XYZXYZXYZ to be changed to the output of the printf as noted?

  • 1
    Could you write a real sample input line, and the output you are expecting to obtain? – Eduardo Trápani Oct 28 '19 at 20:23

Try to put your printf command in double quotes. With the $(…) command substitution syntax and adding single quotes to your sed commands, it would be:

sed 's/XYZXYZXYZ/'"$(printf ' "%s" ' $(sed -n '1,265p' Run-0020))"/ ../../wt/wt-layout_A.dat

You can probably remove the first space character in your printf command changing it to printf '"%s" '.


Your problem is, that you are trying to replace a string with a multi-line output in sed. The replacement string may not have newline characters. They need to be encoded as \n

One workaround could be encoding newlines in your replacement string with an unusual character, then decoding them again - inbetween just add quotes where needed:

 sed \
   #replace newlines in input with END OF TEXT byte as separator
   #not using NUL byte as many shells ignore them
   #ignore newline on last entry
   -e "s/XXX/$(sed -n 1,265p RUNFILE | tr '\n' '\03' | sed 's/\x03$//' )" \
   #replace new separator by "<separator>", as well addin " at beginning and end of line
   -e 's/\x03/"\0"/g;s/^/"/;s/$/"/'
   #reintroduce newlines
   -e 's/\x03/\n/g' LAYOUTFILE > OUTPUT

Sample input




PS: ignoring the use of printf as all you need is double quotes around the strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.