对文件中的字符串块进行排序

对文件中的字符串块进行排序

我有很多咒语和相关描述(如角色扮演游戏),不是按字母顺序排列的。显示的代码是乳胶中的

每个块的第一行必须包含 \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

相关内容