I've been reading something in Most useful sed one-liners for TeX users which is related to my problem, but albeit interesting, it is not what I need.
I have many tex (beamer class) files in subdirectories and I would like to comment some lines in each of them.
The purpose is to modify the preamble in order to compile the files to get the handouts as indicated in Passing parameters to a document answer 45
In each file I have the following lines, which I comment in and out as needed. The first one is to compile the beamer presentation while the others three are needed to compile for handouts
%here is some text, consistent in all the .tex files
%\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer}
%
\documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer}
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
The four lines above should be commented out and the some lines should be appended. The desired .tex file should contains the following
%here is some text, consistent in all the .tex files
\ifdefined\ishandout
\documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer}
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
\else
\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer}
\fi
%\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer}
%
%\documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer}
%\usepackage{pgfpages}
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
Of course I could edit each file manually, but I would learn nothing.
I was successful in inserting the lines from \ifdefined to \fi, by pasting them into a file (saved as insert.txt) and then sending in a terminal (debian wheezy) the following line
sed -i "/^%here is some text, consistent in all the .tex files$/ r ../insert.txt" lesson_10.tex
I tested it only on one .tex file, and it works
To add the lines to each file in each subdir, I wrote this bash script
for i in ??_lesson*/
do
if [ -d $i ]
then
cd $i
THIS=$(ls lesson_??.tex)
sed -i "/^%here is some text, consistent in all the .tex files$/ r ../insert.txt" $THIS
fi
done
Now I need a sed command that change/replace the lines
\documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer}
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
into/with
%\documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer}
%\usepackage{pgfpages}
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
I've read somewhere on the internet that sed process a line at time, so I tried this one.
sed "s/%\documentclass/%\documentclass/g" lesson_01.tex > checkthis.tex; head checkthis.tex
which returns
\%documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer}
\%documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer}
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
This other line was executed to check whether the problem was in the s or in the g part of the command
sed "s/%\documentclass/%\documentclass/g" lesson_01.tex > checkthis.tex; head checkthis.tex
which returns
\%
oo[xcolor={usenames, table, x11names}, final, 10pt]{beamer}
\%
oo[xcolor={usenames, table, x11names}, handout, 10pt]{beamer}
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
It's evident that the "%" has a special meaning for sed, a meaning which I ignore.
I tried also some escaping characters before the %, but to no avail.
Googling around was of no help, I therefore decided to ask for a direct help here.
sed -e 's/^\(\\documentclass\)/\%\1/' -e '/^\%\\documentclass/,/\\pgfuselayout/s/^\(.*pgf\)/\%\1/'(not fully tested so don't use-iuntil you are sure). – cfr Apr 07 '14 at 16:36For cfr: the code works and the outcome is a s follows { %\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer} \documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} % \usepackage{pgfpages} % \pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
– Ottorino Apr 08 '14 at 06:59@cfr: the code works partially. Here's the outcome:
%\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer} \documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} % \usepackage{pgfpages} % \pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]Only one occurrence of\documentclasshas changed.Could you be please so kind to bear with me a little more and explain briefly the syntax ?
– Ottorino Apr 08 '14 at 07:14^\documentclass...while the second one is not at the beginning of the line, but it have some spaces before !!
– Ottorino Apr 08 '14 at 07:31^ \documentclass...