I'm trying to prefix and append blocks of text to a list of files. Here is where I am so far. The sticking point is the sed -i "1i \$prefix" "$file" &&
line. sed
won't substitute the value of prefix
in. I tried to follow various threads about this on unix.sx and a couple of other places, but just got a headache.
Can someone tell me how to fix this? If it is too complicated I'm Ok with using something else instead of sed
.
This is closely related to How do I append text to the beginning and end of multiple text files in Bash?, which is where I got the code below from. The difference is that the question does not cover expanding a variable, and also in my case the strings in question are multiline.
filelist=(foo.tex bar.tex)
prefix='\documentclass[12pt]{article}
\usepackage{myarticle}
%\xpretocmd{\opening}{\insertname}{}{}
\begin{document}
%\insertname
\begin{verbatim}'
suffix='\end{verbatim}
\end{document}'
for file in "${filelist[@]}"; do
sed -i "1i \$prefix" "$file" &&
echo "$suffix" >> "$file"
done
$prefix
. What is it for? – angus Jan 24 '14 at 21:32for file in "${filelist[@]}"; do sed -i "1i $(echo -E "$prefix" | sed -e 's/\\/\\\\/g')" "$file" && echo "$suffix" >> "$file" done
, but this gives an error. – Faheem Mitha Jan 24 '14 at 22:111i\\
NEWLINE$(echo ...
. See the linked answer and write yoursed
line exactly as it is there, only add-i
and change5a
by1i
and$text
by$prefix
. That's all. – angus Jan 25 '14 at 01:04