在bash脚本中使用sed替换LaTeX别名
我对bash脚本编程比较陌生,对LaTeX没有经验.我被要求开发一个脚本,用于替换LaTeX文档中的便捷快捷方式和更加繁琐的长形等价物.
到目前为止,我的方法是在单独的变量中分离快捷方式和长形式,然后尝试使用sed在文本中替换它们.我在下面附上了简短的示例文件. 因为它当前是脚本需要2个参数,一个文件expr,它从中检索快捷方式和长形式术语,以及一个应该进行适当更改的infile.我知道脚本正确地隔离了快捷方式和长格式并且可以返回它们,但它似乎无法执行sed命令. 我尝试在线搜索并发现多个类似的问题,其中建议是sed难以识别变量,并且各种类型的报价组合可能解决问题.我尝试了许多排列,但似乎都没有.在许多情况下,长形式术语包含特殊字符,如’$’和'{}’,所以我怀疑这可能是问题,但我不确定.我也非常愿意接受有关如何解决问题的其他想法.请在下面找到脚本和2个参数文件expr和infile的示例. 包含快捷方式和长格式的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} 更新: 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |