本站教程收集整理的这篇文章主要介绍了在bash脚本中使用sed替换LaTeX别名,本站教程本站觉得挺不错的,现在分享给大家,也给大家做个参考。
我对bash脚本编程比较陌生,对LaTeX没有经验.我被要求开发一个脚本,用于替换LaTeX文档中的便捷快捷方式和更加繁琐的长形等价物.
到目前为止,我的方法是在单独的变量中分离快捷方式和长形式,然后尝试使用sed在文本中替换它们.我在下面附上了简短的示例文件.
因为它当前是脚本需要2个参数,一个文件expr,它从中检索快捷方式和长形式术语,以及一个应该进行适当更改的infile.我知道脚本正确地隔离了快捷方式和长格式并且可以返回它们,但它似乎无法执行sed命令.
我尝试在线搜索并发现多个类似的问题,其中建议是sed难以识别变量,并且各种类型的报价组合可能解决问题.我尝试了许多排列,但似乎都没有.在许多情况下,长形式术语包含特殊字符,如’$’和'{}’,所以我怀疑这可能是问题,但我不确定.我也非常愿意接受有关如何解决问题的其他想法.请在下面找到脚本和2个参数文件expr和infile的示例.
香港vps
包含快捷方式和长格式的expr文件
% a \newcommand{\ao}{$^{18}$O} \newcommand{\aodso}{$^{18}$O/$^{16}$O} % b \newcommand{\bea}{\begin{equation}} \newcommand{\beaa}{\begin{eqnarray}} % c \newcommand{\cthreE}{C$_3$} \newcommand{\cfour}{C$_4$} \newcommand{\coz}{CO$_2$}
infile包含由long-forms替换的快捷方式
This is my test {\ao} {\aodso} my test is this Does it work {\bea} {\beaa} test test test work work work {\cthreE} {\cfour} This is my test my test is this {\coz}
脚本的相关子部分用expr和infile作为参数调用
while read line; do if [[ $line == \newcommand* ]]; then temp=${line#*\{} sc=${temp%%\}*} templf=${temp#*\{} lf=${templf%\}} #echo $sc,$lf sed -i -e 's/${sc}/${lf}/g' ${infilE} fi done < ${expr}
更新:
为了澄清,这就是期望的结果,infile中存在的快捷方式将替换为适当的长形式
This is my test {$^{18}$O} {$^{18}$O/$^{16}$O} my test is this Does it work {\begin{equation}} {\begin{eqnarray}} test test test work work work {C$_3$} {C$_4$} This is my test my test is this {CO$_2$}
解决方法
GNU
sed的代码:
sed -r '/^%/d;s#.*\b(\{\\\w+\})(\{.*\})#\1 \2#;s#\\#\\\\#g;s#(\S+)\s(\S+)#\\|\1|s|\1|\2|g#' file1|sed -f - file2
$cat file1 % a \newcommand{\ao}{$^{18}$O} \newcommand{\aodso}{$^{18}$O/$^{16}$O} % b \newcommand{\bea}{\begin{equation}} \newcommand{\beaa}{\begin{eqnarray}} % c \newcommand{\cthreE}{C$_3$} \newcommand{\cfour}{C$_4$} \newcommand{\coz}{CO$_2$} $cat file2 This is my test {\ao} {\aodso} my test is this Does it work {\bea} {\beaa} test test test work work work {\cthreE} {\cfour} This is my test my test is this {\coz} $sed -r "/^%/d;s#.*\b(\{\\\w+\})(\{.*\})#\1 \2#;s#\\#\\\\#g;s#(\S+)\s(\S+)#\\|\1|s|\1|\2|g#" file1|sed -f - file2 This is my test {$^{18}$O} {$^{18}$O/$^{16}$O} my test is this Does it work {\begin{equation}} {\begin{eqnarray}} test test test work work work {C$_3$} {C$_4$} This is my test my test is this {CO$_2$}
说明:
有两个sed调用,第一个调用带有搜索/替换模式的文件和sed脚本:
sed -r '/^%/d;s#.*\b(\{\\\w+\})(\{.*\})#\1 \2#;s#\\#\\\\#g;s#(\S+)\s(\S+)#\\|\1|s|\1|\2|g#' file1 \|{\\ao}|s|{\\ao}|{$^{18}$O}|g \|{\\aodso}|s|{\\aodso}|{$^{18}$O/$^{16}$O}|g \|{\\bea}|s|{\\bea}|{\\begin{equation}}|g \|{\\beaa}|s|{\\beaa}|{\\begin{eqnarray}}|g \|{\\cthreE}|s|{\\cthreE}|{C$_3$}|g \|{\\cfour}|s|{\\cfour}|{C$_4$}|g \|{\\coz}|s|{\\coz}|{CO$_2$}|g
在第二次调用中,sed使用文本文件处理此脚本以进行替换.
sed -f - file2
本站总结
以上是本站教程为你收集整理的在bash脚本中使用sed替换LaTeX别名全部内容,希望文章能够帮你解决在bash脚本中使用sed替换LaTeX别名所遇到的程序开发问题。
如果觉得本站教程网站内容还不错,欢迎将本站教程推荐给好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。