从tex匹配不同行上的多个正则表达式组以打印到csv中

问题描述 投票:1回答:2

我有一个投影仪乳胶文件,在该文件中某些帧的格式为

\frame{\frametitle{Title01}
Sub01\\
\begin{tabular}{|p{7cm}|}
\hline
\rowcolor{black}\\
\rowcolor{white}\\
\rowcolor{green}\\
\hline
\end{tabular}
}

我想以类似的csv格式结束

Title01,Sub01,black,white,green
Title02,Sub02,red,white,blue

到目前为止,我已经设法获得所有标题

sed -rn 's/^.*frametitle\{(.*)\}/\1,/pm' f.tex

我无法在下一行中匹配第二组Sub01(目前使用Latexlinebreak \),这是我到目前为止尝试过的一小部分

sed -rn 's/^.*frametitle\{(.*)\}\n(.*)$/\1,\2/mp' f.tex
sed -rn 's/^.*frametitle\{(.*)\}$^(.*)$/\1,\2/mp' f.tex
sed -rn 's/^.*frametitle\{(.*)(\}\n)(.*)$/\1,\3/mp' f.tex
sed -rn 's/^.*frametitle\{(.*)\}\n(.*)\n/\1,\2/mp' f.tex

全部匹配标题,或完全不匹配。

regex csv sed latex multiline
2个回答
2
投票

这可能对您有用(GNU sed):

sed -n '/^\\frame{\\frametitle{\(.*\)}.*/{s//\1/;h;n;s/\([^\]*\).*/\1/;H;:a;n;/^\\rowcolor{\(.*\)}.*/{s//\1/;H};/^}/!ba;g;s/\n/,/gp}' file

这是一项过滤作业,因此使用-n选项仅打印所需的内容。

所需的数据存在于以\frame{\frametitle{...}开头和以}开头的行之间。

使用上述标准,将所需的匹配数据复制到保留空间,并在遇到匹配结束时,用此复制的数据替换当前行。

数据将由换行符分隔,因此请用逗号替换并打印出结果。


0
投票

类似于在[[多行模式中使用使用此功能:

perl -0ne ' push @a, $_ for /.*?frametitle\{(\w+)\}\R # first line (\w+) # second line .*rowcolor\{(\w+).*rowcolor\{(\w+).*rowcolor\{(\w+) # other lines /sx; END{print join ",", @a} ' file
© www.soinside.com 2019 - 2024. All rights reserved.