2017-06-28 98 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