I've got a script with a sed command in it that i use to preprend some text to the top of a file.
sed '1s|^|'"$name"' \
'"$(usageblock "$name" "$desc")"' \
|' "$dest" > "${dest}.new" && \
mv "$dest" "${dest}.bak" && \
mv "${dest}.new" "$dest" && \
rm "${dest}.bak"
As you can see, that script is calling a usageblock()
function with arguments (function which is defined in another file) that prints out the actual text needed, and for that i use printf
and its nice formatting capabilities.
local format
format="%-15s %-4s %-7s\n"
printf "$format" "Description:" "" "$desc"
The problem is in the format variable, the \n
, more precisely. Once I call that function within the sed
command, it throws out:
unescaped newline inside substitute pattern
And as soon as I delete that \n
, it prints out fine (except that there is no new line at the end of every printf statement, which defeats the purpose of using printf in the first place).
So for the moment, I modified my function to print out using echo instead of printf but I don't like it. I have to use the echo
command for every line that I need to print. Ugly.
So the question is, how can I use printf formatting capabilities for printing out to sed without causing problems with unescaped new lines? I've tried:
format="%-15b %-4b %-7b\n \\" OR
format="%-15b %-4b %-7b \\" OR
printf "$format" "Description:" "" "$desc \\"
and i've tried different combinations but simply nothing works. It seems like sed needs a single thing and is for me to take the \n
part out of the format altogether.
Anybody knows about a sed or a printf flag to use in that case?