ファイル内の文字列の塊を並べ替える

ファイル内の文字列の塊を並べ替える

アルファベット順ではなく、注文や関連する説明(ロールプレイングゲームなど)がたくさんあります。表示されるコードはラテックスです。

各ブロックの最初の行には\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

おすすめ記事