@JonathanLeffler 为您的特定问题提供了一个很好的 awk 答案,但如果您要经常处理具有该格式的文件,您可能需要考虑重新格式化它们以使记录由换行符分隔,每个列表项都在一行,例如:
$ cat file
DSL -
1. Digital Simulation Language. Extensions to FORTRAN to simulate analog
computer functions. "DSL/90 - A Digital Simulation Program for Continuous
System Modelling", Proc SJCC 28, AFIPS (Spring 1966). Version: DSL/90 for
the IBM 7090. Sammet 1969, p.632.
FLIP -
1. Early assembly language on G-15. Listed in CACM 2(5):16 (May 1959).
2. "FLIP User's Manual", G. Kahn, TR 5, INRIA 1981.
3. Formal LIst Processor. Early language for pattern-matching on LISP
structures. Similar to CONVERT. "FLIP, A Format List Processor", W.
Teitelman, Memo MAC-M-263, MIT 1966.
$ awk '!/^[[:space:]]*$/{printf "%s%s", (NF==2 && /-[[:space:]]*$/ ? rs rs : (/^ +[[:digit:]]+\./ ? rs : "")), $0; rs="\n"} END{print ""}' file
DSL -
1. Digital Simulation Language. Extensions to FORTRAN to simulate analogcomputer functions. "DSL/90 - A Digital Simulation Program for ContinuousSystem Modelling", Proc SJCC 28, AFIPS (Spring 1966). Version: DSL/90 forthe IBM 7090. Sammet 1969, p.632.
FLIP -
1. Early assembly language on G-15. Listed in CACM 2(5):16 (May 1959).
2. "FLIP User's Manual", G. Kahn, TR 5, INRIA 1981.
3. Formal LIst Processor. Early language for pattern-matching on LISPstructures. Similar to CONVERT. "FLIP, A Format List Processor", W.Teitelman, Memo MAC-M-263, MIT 1966.
这样您就可以轻松处理输出以打印或做任何您想做的事情,例如
1)打印每个标题行加上第一个项目符号:
$ awk '...' file | awk 'BEGIN{RS=""; ORS="\n\n"; FS=OFS="\n"} {print $1,$2}'
DSL -
1. Digital Simulation Language. Extensions to FORTRAN to simulate analogcomputer functions. "DSL/90 - A Digital Simulation Program for ContinuousSystem Modelling", Proc SJCC 28, AFIPS (Spring 1966). Version: DSL/90 forthe IBM 7090. Sammet 1969, p.632.
FLIP -
1. Early assembly language on G-15. Listed in CACM 2(5):16 (May 1959).
2)打印标题行加上“FLIP”记录的第二个项目符号:
$ awk '...' file | awk 'BEGIN{RS=""; ORS="\n\n"; FS=OFS="\n"} /^FLIP -/{print $1,$3}'
FLIP -
2. "FLIP User's Manual", G. Kahn, TR 5, INRIA 1981.
3)打印标题行加上该标题的项目符号数:
$ awk '...' file | awk 'BEGIN{RS=""; FS=OFS="\n"} {print $1 NF-1}'
DSL - 1
FLIP - 3
等等等等