2017-06-28 38 views
3

Ich möchte Fußnoten aus einem LaTeX-Dokument mit einem Bash-Skript herausfiltern. Es mag wie eine dieser beiden Beispiele aussehen:Passende Klammern über mehrere Zeilen (mit 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.} 

Die Überreste sollten:

Some text with a short footnote. 

Some text with a longer footnote.% 

Ich kümmere mich nicht um zusätzliche Leerzeichen.

Da übereinstimmende Klammern mit regulären Ausdrücken nicht möglich sind, nehme ich an, dass ich sed dafür nicht verwenden kann. Ist es möglich mit awk oder einem anderen Werkzeug?

Antwort

1

Mit GNU awk für Multi-char RS und null FS Aufspaltung der Datensatz in Zeichen:

$ 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

Mit rekursiven regex in der Befehlszeile perl, können Sie passende Klammern wie dieses Spiel:

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

Some text with a short footnote. 

Some text with a longer footnote. 

Für regex Details here is regex demo