使用sed从LaTeX文件中删除文本

时间:2015-12-29 20:03:56

标签: regex bash unix sed

我试图通过消除两个给定字符串之间的文本来从LaTeX文件中删除一些冗长的段落。我已经找到了很多关于使用sed的建议,但我在确定要使用的确切语法方面遇到了一些麻烦。例如,我想要这个文件:

\maketitle
\section{Part 1}
Here is some text
\section{Part 2}
Here is some more text

并将其转换为此文件:

\maketitle
\section{part 2}
Here is some more text

我遇到的麻烦是sed被反斜杠搞糊涂了。我应该使用适当的转义字符,还是有更好的解决方案?

3 个答案:

答案 0 :(得分:0)

您可以使用字符类来阻止对斜杠的解释,也可以进一步转义,例如:

'mongodb://'+ MONGODB[ 'username' ] + ':' + MONGODB[ 'password' ] + '@' + MONGODB['server'] + ':' + MONGODB['port'] + '/' + MONGODB['database']

使用/输出

sed '/^[\]section[{]Part 1[}]/, /^Here is some text/d' filename

答案 1 :(得分:0)

这可能适合你(GNU sed):

sed '/^\\maketitle/,/^\\section{Part 2}/{//!d}' file

或者如果您想删除特定部分:

sed '/^\\/h;G;/^\\section{Part 1}/!P;d' file

答案 2 :(得分:0)

这是一个非常简单,适应性强且直接的变体(例如,除了两个部分标题之外没有任何引用):

sed '/^\\section{Part 1}/,/^\\section{Part 2}/ {/^\\section{Part 2}/!d;}'

简而言之,这表示:在"第1部分和第34部分的行中。标题为"第2部分"标题包含,删除所有这些除了"第2部分"报头中。