2017-06-28 60 views
3

我想从使用bash脚本一个LaTeX文档过滤掉脚注。它外观可能为这些例子:匹配括号多行(使用awk?)

Some text with a short footnote.\footnote{Some \textbf{explanation}.} 

Some text with a longer footnote.% 
    \footnote{Lorem ipsum dolor 
    sit amet, etc. etc. etc. \emph{along \emph{multiple} lines} 
    but all lines increased indent from the start.} 

遗骸应该是:

Some text with a short footnote. 

Some text with a longer footnote.% 

我不关心多余的空白。

由于匹配的括号不能使用正则表达式来完成,我相信我不能使用sed这一点。是否有可能与awk或一些其他的工具?

回答

1

随着GNU AWK多焦RS和空FS分割记录成字符:

$ cat tst.awk 
BEGIN { RS="[\\\\]footnote"; ORS=""; FS="" } 
NR>1 { 
    braceCnt=0 
    for (charPos=1; charPos<=NF; charPos++) { 
     if ($charPos == "{") { ++braceCnt } 
     if ($charPos == "}") { --braceCnt } 
     if (braceCnt == 0) { break } 
    } 
    $0 = substr($0,charPos+1) 
} 
{ print } 

$ awk -f tst.awk file 
Some text with a short footnote. 

Some text with a longer footnote.% 
2

在命令行perl使用递归的正则表达式,你可以匹配匹配括号,因为这:

perl -00pe 's/%?\s*\\footnote({(?:[^{}]*|(?-1))*})//g' file 

Some text with a short footnote. 

Some text with a longer footnote. 

对于正则表达式的细节here is regex demo