使用的sed在bash脚本来代替乳胶别名 [英] Using sed in bash script to replace LaTeX aliases

查看:151
本文介绍了使用的sed在bash脚本来代替乳胶别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的bash脚本编程,并有乳胶没有经验。我一直要求制定一个脚本,将替换LaTeX文档的方便快捷与他们更加麻烦的长篇当量。

我的方法迄今已隔离两个快捷方式和长期形成的在不同的变量,然后尝试通过SED,以取代他们的文字。我们已经附上简短的例子下面的文件。

由于它是目前脚本需要两个参数,从中检索快捷方式和长期形成的术语和INFILE到这是应该做适当的修改文件EXPR。我知道脚本正确隔离既快捷方式和长期的形式,并且可以返回他们,但它似乎无法执行sed命令。

我试图寻找这个网上,发现多个类似的问题,其中的建议是,战略经济对话难以确认变量和各种类型的报价组合可能会解决这个问题。我已经尝试了许多排列,没有出现一起工作。在许多情况下长期形成的术语包含特殊字符,如$和{},让我怀疑这可能是问题,但我不知道。我也很开放,如何解决这个问题其他的想法。请发现无论是剧本的样品和2参数文件,EXPR和INFILE下方。

包含快捷方式和长期的形式EXPR文件

一%
\\ newcommand {\\ AO} {$ ^ {18} $ O]
\\ newcommand {\\ aodso} {$ ^ {18} $ O / $ ^ {16} $ O]
%b
\\ newcommand {\\ BEA} {\\ {开始公式}}
\\ newcommand {\\ beaa} {\\ {开始} eqnarray的}
% C
\\ newcommand {\\ cthree} {C $ _3 $}
\\ newcommand {\\ cfour} {C $ _4 $}
\\ newcommand {\\堂妹} {$ CO _2 $}

INFILE包含快捷方式通过长期的形式来代替

这是我的测试{\\} AO
{\\ aodso}我的测试是这样的
它的工作原理{\\ BEA}
{\\ beaa}测试测试测试
工作工作工作{\\ cthree}
{\\ cfour}这是我的测试
我的测试是这样的{\\}堂妹

脚本的相关小节调用EXPR和INFILE作为参数

 而读线;做
    如果[[$线== \\ newcommand *];然后
    TEMP = $ {行#* \\ {}
    SC = $ {气温%% \\} *}
    templf = $ {#温度* \\ {}
    LF = $ {templf%\\}}
    #echo $ SC,$ LF
    SED -i -e'S / $ {SC} / $ {} LF / g的$ {} infile中
    科幻
完成< $ {expr}的

更新:
对于澄清,这是理想的结果会是什么,快捷美元INFILE p $ psent将与适当的长形式取代

这是我的测试{$ ^ {18} $ O]
{$ ^ {18} $ O / $ ^ {16} $ O]我的测试是这样的
它的工作原理{\\ {开始公式}}
{\\ {开始} eqnarray的}测试测试测试
工作工作工作{C $ _3 $}
{C $ _4 $}这是我的测试
我的测试是这样{$ CO _2 $}


解决方案

$ C $为GNU的 SED

SED -r'/ ^%/天; S#* \\ B(\\ {\\\\\\ W + \\} )(\\)#\\ 1 \\ 2#; S#\\\\#\\\\\\\\#克; {* \\。}等#(\\ S +)\\ S(\\ S +)#\\\\ | \\ 1 | S | \\ 1 | \\ 2 | G#'文件1 | sed的-f - 文件2



$ file1的猫
% 一个
\\ newcommand {\\ AO} {$ ^ {18} $ O]
\\ newcommand {\\ aodso} {$ ^ {18} $ O / $ ^ {16} $ O]
%b
\\ newcommand {\\ BEA} {\\ {开始公式}}
\\ newcommand {\\ beaa} {\\ {开始} eqnarray的}
% C
\\ newcommand {\\ cthree} {C $ _3 $}
\\ newcommand {\\ cfour} {C $ _4 $}
\\ newcommand {\\堂妹} {$ CO _2 $}$ file2的猫
这是我的测试{\\} AO
{\\ aodso}我的测试是这样的
它的工作原理{\\ BEA}
{\\ beaa}测试测试测试
工作工作工作{\\ cthree}
{\\ cfour}这是我的测试
我的测试是这样的{\\}堂妹$ sed的-r/^%/d;s#.*\\b(\\{\\\\\\w+\\})(\\{.*\\})#\\1 \\ 2#; S#\\\\#\\\\\\ \\ #G; S#(\\ S +)\\ S(\\ S +)#\\\\ | \\ 1 | S | \\ 1 | \\ 2 | G#文件1 | sed的-f - 文件2
这是我的测试{$ ^ {18} $ O]
{$ ^ {18} $ O / $ ^ {16} $ O]我的测试是这样的
它的工作原理{\\ {开始公式}}
{\\ {开始} eqnarray的}测试测试测试
工作工作工作{C $ _3 $}
{C $ _4 $}这是我的测试
我的测试是这样{$ CO _2 $}


说明:

有用于 SED 两个电话,第一个从搜索文件使/替换模式的 SED 脚本:


SED -r'/^%/d;s#.*\\b(\\{\\\\\\w+\\})(\\{.*\\})#\\1 \\ 2#; S#\\\\#\\\\\\\\ #G; S#(\\ S +)\\ S(\\ S +)#\\\\ | \\ 1 | S | \\ 1 | \\ 2 | G#'文件1
\\ | \\\\ {} AO | S | {\\\\ AO} | {$ ^ {18} $ O] | G
\\ | \\\\ {} aodso | S | {\\\\ aodso} | {$ ^ {18} $ O / $ ^ {16} $ O] | G
\\ | \\\\ {} BEA | S | {\\\\ BEA} | {\\\\ {开始公式}} | G
\\ | \\\\ {} beaa | S | {\\\\ beaa} | {\\\\ {开始} eqnarray的} | G
\\ | \\\\ {} cthree | S | {\\\\ cthree} | {C $ _3 $} | G
\\ | \\\\ {} cfour | S | {\\\\ cfour} | {C $ _4 $} | G
\\ | \\\\ {}堂妹| S | {\\\\堂妹} | {$ CO _2 $} | G

在第二个电话 SED 处理这个脚本的文本文件,使替代品。

SED -f - 文件2

I am relatively new to bash scripting and have no experience with LaTeX. I've been asked to develop a script which will replace convenience shortcuts in LaTeX documents with their more cumbersome long-form equivalents.

My approach thus far has been to isolate both the shortcut and the long-form in separate variables and then try to replace them in the text by using sed. I've attached short example files below.

As it is currently the script takes 2 arguments, a file expr from which it retrieves the shortcuts and long-form terminology and an infile to which is is supposed to make the appropriate changes. I know that the script is properly isolating both the shortcuts and long-forms and can return them, but it can't seem to execute the sed command.

I have tried searching this online and found multiple similar question where the suggestion was that sed has difficultly recognizing variable and that various type of quotation combinations might solve the problem. I have tried many permutations and none appear to work. The long-form terminologies in many cases contain special characters such as '$' and '{}', so I suspect that this might be the issue but I'm not sure. I am also very much open to other ideas about how to solve the problem. Please find below samples of both the script and the 2 argument files, expr and infile.

expr file containing shortcuts and long-forms

% 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 containing shortcuts to be replaced by 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}

Relevant subsection of script called with expr and infile as arguments

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}

UPDATE: For clarification, this is what the desired result would be, the shortcuts present in infile would be substituted with the appropriate long-form

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$}

解决方案

Code for GNU :

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$}


Explanation:

There are two calls for sed, the first one makes from the file with the search/replace patterns a sed script:

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

In the second call sed processes this script with the text file to make the replacements.

sed -f - file2

这篇关于使用的sed在bash脚本来代替乳胶别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆