Following the idea of PhelypeOleinik, but without having to comment all the lines, perhaps the perl script srcredact (part of TeXLive 2019) can be of help. This is my modified example file (MdAyq6-doc.tex):
%<*ALL>
\documentclass{article}
\begin{document}
Common text
%</ALL>
%<*techrep>
Stuff for the TR
%</techrep>
%<*maindoc>
Stuff for the main paper
%</maindoc>
%<*ALL>
\end{document}
%</ALL>
Now from the command line:
[pablo@worktex forum] $ srcredact -l MdAyq6-doc.tex
ALL
maindoc
techrep
[pablo@worktex forum] $ srcredact -e techrep MdAyq6-doc.tex
\documentclass{article}
\begin{document}
Common text
Stuff for the TR
\end{document}
[pablo@worktex forum] $ srcredact -e maindoc MdAyq6-doc.tex
\documentclass{article}
\begin{document}
Common text
Stuff for the main paper
\end{document}
[pablo@worktex forum] $ srcredact -e ALL MdAyq6-doc.tex
\documentclass{article}
\begin{document}
Common text
\end{document}
Here we see the output on screen, if we want to generate the output file just add > file.tex at the end of the line. As follows:
$ srcredact -e techrep MdAyq6-doc.tex > techrep.tex
and the file techrep.tex will be created.
\documentclass{article}
\begin{document}
Common text
Stuff for the TR
\end{document}
It's not as powerful as docstript, but it does a great job.
EDIT
If the idea is to remove using regular expressions, it's not that easy, the escaped braces inside are a mess, but, something can be done using Perl :). Assuming that the syntax of your input file is valid, with the following rmifthen.pl script it is possible:
#!/usr/bin/env perl
use v5.26;
use autodie;
use File::Basename;
use open ':locale';
## Args
@ARGV == 1 or die "Use: $0 <file (La)TeX to be processed>\n";
my $input_file = shift;
-f $input_file or die "ERROR: Can't find [$input_file]\n";
## Extension
my @SuffixList = ('.tex', '', '.ltx');
my ($name, $path, $ext) = fileparse($input_file, @SuffixList);
$ext = '.tex' if not $ext;
## Read file
open my $TEXINPUT, '<', $input_file;
my $ltxfile;
{
local $/;
$ltxfile = <$TEXINPUT>;
}
close $TEXINPUT;
## Change escaped braces to a character that is not in the text
$ltxfile =~ s/\\[{]/«/g;
$ltxfile =~ s/\\[}]/»/g;
## Two groups of captures: the complete ifthenelse, and the true ifthenelse.
my $ifthenelse = qr/
(
\\ifthenelse (?&condicion_rx)\s*
(?<expVerdadero> (?&expVerdadero_rx)\s*)
(?&expFalsa_rx)
)
(?(DEFINE)
(?<condicion_rx> (?&texto_rx))
(?<expVerdadero_rx> (?&texto_rx))
(?<expFalsa_rx> (?&texto_rx))
(?<texto_rx> ( [{] (?: [^{}]++ | (?-1) )*+ [}] ) )
)
/sx;
## We look for ifthenelse throughout the document
my @posiciones;
# Every time we find an ifthenelse mark, we keep the initial, final
# position of the whole mark, And the text of the true clause
while ($ltxfile =~ /$ifthenelse/g) {
push @posiciones, [ $-[1], $+[1], $+{expVerdadero} ];
}
## We replace the whole ifthenelse brand, with the true expression, except the braces
for my $pos_ref (reverse @posiciones) {
substr($ltxfile, $pos_ref->[0], $pos_ref->[1] - $pos_ref->[0])
= substr $pos_ref->[2], 1, -2;
}
## We recovered the escaped braces.
$ltxfile =~ s/«/\\{/g;
$ltxfile =~ s/»/\\}/g;
## Write file
open my $SALIDA, '>', "$name-out$ext";
print $SALIDA $ltxfile;
close $SALIDA;
__END__
With this example file:
\RequirePackage{ifthen}
\newboolean{techrep}
\setboolean{techrep}{false}
\begin{document}
\ifthenelse{\boolean{techrep}}{Este es texto
con el que {deseo quedarme} incluidas las llaves \{ que no están
escapadas dentro de el, pero sin las llaves exteriores
}{Este es texto NO lo deseo, también {puede llevar llaves} \}
que no están escapadas} %
TEXTO TEXTO
TEXTO TEXTO \ifthenelse{\boolean{techrep}}{Otro texto
con el que \{deseo quedarme\} incluidas las llaves \{ \}
que no están escapadas
}{Este es texto {NO lo deseo}, también puede llevar llaves \{
que no están \} escapadas}
TEXTO TEXTO TEXTO
\end{document}
running:
perl rmifthen.pl file.tex
we get:
\RequirePackage{ifthen}
\newboolean{techrep}
\setboolean{techrep}{false}
\begin{document}
Este es texto
con el que {deseo quedarme} incluidas las llaves \{ que no están
escapadas dentro de el, pero sin las llaves exteriores %
TEXTO TEXTO
TEXTO TEXTO Otro texto
con el que \{deseo quedarme\} incluidas las llaves \{ \}
que no están escapadas
TEXTO TEXTO TEXTO
\end{document}
With your MWE the output is what you propose. I think this is more than you were looking for.
Greetings and good luck with your next conference.
docstrip? See: https://tex.stackexchange.com/a/461982/134574 – Phelype Oleinik Sep 16 '19 at 12:03\ifthenelsecould be replaced by%<techrep>Stuff for the TR[imagine a line break here]%<paper>Stuff for the main paper. And things common to both would be delimited by%<*techrep|paper>and%</techrep|paper>. Adding a third version would be almost trivial. I know your question is not aboutdocstrip. I was just showing the tool :-) Anyhow, I think you would find more sed/awk experts at stackoverflow or unix & linux... – Phelype Oleinik Sep 16 '19 at 12:10