파일의 문자열 덩어리 정렬

파일의 문자열 덩어리 정렬

알파벳 순서가 아닌 주문과 관련 설명(예: 롤플레잉 게임)이 많이 있습니다. 표시된 코드는 라텍스입니다.

각 블록의 첫 번째 줄에는 \index{Spells}가 포함되어야 합니다.

\medskip\textbf{Aid}\index[Spells]{Aid}\\
\textbf{School}: Heal, Necromancy\\
\textbf{Level}: 2, Uncommon\\
\textbf{Cast Time}: 2 Actions\\
\textbf{Range}: 9 meters\\
\textbf{Components}: V, S, M (a thin strip of white bread)\\
\textbf{Duration}: 1 hour for Magic Competency\\
Your spell increases the toughness and resolve of your allies. Choose up to three creatures within range. For the duration, each target's maximum hit points and current hit points increase by 5.\\
\textbf{For each magical critical success rolled} in the Magic Test the target's hit points are increased by an additional 5 points

\medskip\textbf{Halucination of Death}\index[Spells]{Halucination of Death}\\
\textbf{School}: Illusion\\
\textbf{Level}: 4, Uncommon\\
\textbf{Cast Time}: 2 Actions\\
\textbf{Range}: 36 meters\\
\textbf{Components}: V, S\\
\textbf{Duration}: Instant\\
You draw upon the nightmares of a creature within range and that you can see, and create an illusory manifestation of its deepest fears, visible only to that creature. The target must make a Will save. \\
On a failed save, the target is frightened for 1 minute and takes 4d10 damage. \\
\textbf{For each magical critical success rolled} in the Magic Test the damage increases by 1d10

\medskip\textbf{Alarm}\index[Spells]{Alarm}\\
\textbf{School}: Abjuration\\
\textbf{Level}: 1, Common\\
\textbf{Cast Time}: 1 minute\\
\textbf{Range}: 9 meters\\
\textbf{Components}: V, S, M (a bell)\\
\textbf{Duration}: 8 hours\\
Set up an alarm against unwanted intrusions. Choose a door, window, or area within range that is no larger than a 6 meters cube. Until the spell ends, you will be warned by an alarm whenever a creature of Tiny size or larger comes into contact with or enters the protected area.....

잠깐... 인덱스 값에서 이름별로 블록을 정렬해야 합니다(예: \index[Spells]{Alarm} ).
이 경우 순서는 다음과 같습니다.

\medskip\textbf{Aid}\index[Spells]{Aid}..and relative text..\\
\medskip\textbf{Alarm}\index[Spells]{Alarm}....and relative text...\\
\medskip\textbf{Halucination of Death}... and relative text....\\```


how to do on linux ?

thanks

BHH

답변1

GNU awk를 사용하여 정규식 일치에서 철자 이름을 캡처하여 배열 인덱스로 사용한 다음 해당 인덱스를 기준으로 배열을 정렬합니다.

gawk '
  BEGIN {
    RS=""
    FS="\n"
  }
  match($1,/\\index\[Spells\]\{([^}]*)\}/,m) {
    spells[m[1]] = $0
  }
  END {
    PROCINFO["sorted_in"] = "@ind_str_asc"
    ORS="\n\n"
    for(i in spells) print spells[i]
  }
' example.tex

관련 정보