sed를 사용하여 Latex 단락 형식 지정

sed를 사용하여 Latex 단락 형식 지정

저는 sedLaTeX를 사용하여 컴파일하기 위해 여러 일반 텍스트 파일의 형식을 다시 지정하는 작업을 하고 있습니다. 파일에는 범위를 제공하는 키워드가 포함된 섹션이 포함되어 있습니다. 제가 작업하고 있는 부분 중 하나는 찬송가입니다. 찬송가는 키워드로 시작해서 Hymn단어로 끝납니다. Amen. 찬송가의 본문은 "절"의 문맥에 따라 형식을 취해야 합니다.

다음은 샘플 입력 파일입니다.

Hymn I
Conditor alme siderum,
Aeterna lux credentium,
Christe, redemptor omnium,
Exaudi preces supplicum.

Qui condolens interitu
Mortis perire saeculum,
Salvasti mundum languidum,
Donans reis remedium.

Vergente mundi vespere,
Uti sponsus de thalamo,
Egressus honestissima
Virginis matris clausula. Amen.

Hymn II
...
... Amen.

파일이 다음과 같기를 원합니다.

\small{\uppercase{Hymn I}}\normalsize
\begin{verse}
Conditor alme siderum,\\
Aeterna lux credentium,\\
Christe, redemptor omnium,\\
Exaudi preces supplicum.\\!

Qui condolens interitu\\
Mortis perire saeculum,\\
Salvasti mundum languidum,\\
Donans reis remedium.\\!

Vergente mundi vespere,\\
Uti sponsus de thalamo,\\
Egressus honestissima\\
Virginis matris clausula. Amen.\\!
\end{verse}
\small{\uppercase{Hymn II}}\normalsize
\begin{verse}
...\\
... Amen.\\!
\end{verse}

나는 그것의 대부분을 알아낼 수 있었다. 하지만 sed여러 줄을 사용하여 단락의 서식을 올바르게 지정하는 방법을 알고 싶습니다 .

sed '/Hymn/,/Amen/ { /Hymn\|Amen/ !{...' 나는 And H또는 접근 방식을 시도했지만 N결코 제대로 작동하지 않는 것 같습니다.

답변1

이 같은:

sed '/Hymn/,/Amen/{                                 # in this range
/Hymn/{                                             # if line matches Hymn
s/.*/\\small\{\\uppercase\{&\}\}\\normalsize/       # replace as required
h                                                   # copy over the hold space
s/.*/\\begin\{verse\}/                              # replace with \begin{verse}
H                                                   # append to hold space
d                                                   # delete the line
}
/Amen/!{                                            # if line doesn't match Amen 
/^$/!{                                              # and if line is not empty
s/$/\\\\/                                           # add trailing \\
}
H                                                   # append to hold space
d                                                   # then delete line
}
//{                                                 # if line matches Amen
s/$/\\\\!/                                          # add trailing \\!
H                                                   # append to hold space
s/.*/\\end\{verse\}/                                # replace with \end{verse}
H                                                   # append to hold space
s/.*//                                              # empty pattern space
x                                                   # exchange buffers
s/\n\n/!&/g                                         # add ! at end of each para
}
}
' infile

또는 두 줄을 선호하는 경우 gnu sed:

sed '/Hymn/,/Amen/{/Hymn/{s/.*/\\small\{\\uppercase\{\&\}\}\\normalsize/;h;s/.*/\\begin\{verse\}/;H;d}
/Amen/!{/^$/!{s/$/\\\\/};H;d};//{s/$/\\\\!/;H;s/.*/\\end\{verse\}/;H;s/.*//;x;s/\n\n/!&/g}}' infile

예제 출력:

\small{\uppercase{Hymn I}}\normalsize
\begin{verse}
Conditor alme siderum,\\
Aeterna lux credentium,\\
Christe, redemptor omnium,\\
Exaudi preces supplicum.\\!

Qui condolens interitu\\
Mortis perire saeculum,\\
Salvasti mundum languidum,\\
Donans reis remedium.\\!

Vergente mundi vespere,\\
Uti sponsus de thalamo,\\
Egressus honestissima\\
Virginis matris clausula. Amen.\\!
\end{verse}

\small{\uppercase{Hymn II}}\normalsize
\begin{verse}
...\\
... Amen.\\!
\end{verse}

답변2

perl단락 모드에서 사용 하겠습니다 .

#!/usr/bin/env perl
# Usage: thisprogram < inputfile > outputfile
use strict;
use warnings;
use feature qw(say);

$/ = "\n\n";    # paragraph mode

while (<>) {
  # nuke last \n of paragraph due to
  s/[\n]+\z//;
  # the need to replace the mid-verse newlines with \\
  s/\n/\\\\\n/g;
  # and now actually undo that while working on the first line
  s/(Hymn \w+)[\\]+/\\small{\\uppercase{$1}}\\normalsize\n\\begin{verse}/;
  # and at the newline-free end of the paragraph, tack on the \\! bit
  s/\z/\\\\!/;
  # emit
  say;
  if (m/Amen\./) {
    say "\\end{verse}\n";
  } else {
    print "\n";
  }
}

관련 정보