如何使用awk从txt文件中提取每个段落的第一段?
How to extract the first segment of each paragraph from a txt file using awk?
我有一个 txt 文件,只有 10,000 多行,在这些行中有一个我需要提取的编程语言列表,它是这样的:
P+ - "Experience with Remote Procedure Calls in a Real-Time Control System", B. Carpenter et al, Soft Prac & Exp 14(9):901-907 (Sep 1984).
P4 - Rusty Lusk lusk@anta.mcs.anl.gov. A macro/subroutine package
for parallel programming, using monitors on shared memory machines,
message passing on distributed memory machines. Implemented as a
subroutine library for C and Fortran. An enhancement of the "Argonne
macros", PARMACS. ftp://info.mcs.anl.gov/pub/p4t1.2.tar.Z info:
p4@mcs.anl.gov
PABC - Intermediate language recognized by the Parallel ABC machine,
used in the implementation of Concurrent Clean. "The PABC Simulator",
E.G.J.M.H. NM-^Zecker, TR 89-19, U Nijmegen 1989.
我只需要提取每种语言的名称,避免其他所有内容,并考虑到某些名称有多个单词,所以我尝试使用“-”作为分隔符。但是我找不到如何正确地做到这一点。
首先我尝试了:
awk '{ print }' RS="\n\n" ORS= language.TXT
或awk '{ print }' RS= ORS="\n\n" language.TXT
但唯一的输出是文件的第一个单词:
The
我也做了:
$ awk -F "-" '{ print }' language.TXT
它确实给了我每个名字,但由于它也包含描述的每一行,它输出的内容类似于(与上面的示例相比):
+
System", B. Carpenter et al, Soft Prac & Exp 14(9):901
P4
parallel programming, using monitors on shared memory machines, message
passing on distributed memory machines. Implemented as a subroutine
library for C and Fortran. An enhancement of the "Argonne macros",
PARMACS.
ftp://info.mcs.anl.gov/pub/p4t1.2.tar.Z
info: p4@mcs.anl.gov
PABC
in the implementation of Concurrent Clean. "The PABC Simulator",
E.G.J.M.H. NM
使用 awk 的“paragrah 模式”执行此操作的正确方法是什么?
请注意,我正在使用 gawk
Using any awk in any shell on every Unix box, this is how to use awks paragraph mode:
$ awk -v RS= '{print }' file
P+
P4
PABC
以上假设您想要输出的字符串中 none 可以包含空格,因为您没有在样本输入中包含任何空格如果这些字符串可以包含空格,那么这可能就是您需要的如果它们不能包含 <blank>-
:
$ awk -v RS= -F'(^|\n) *| +-' '{print }' file
P+
P4
PABC
如果它们可以包含 <blank>-
那么您需要告诉我们如何在输入中识别它们。
我有一个 txt 文件,只有 10,000 多行,在这些行中有一个我需要提取的编程语言列表,它是这样的:
P+ - "Experience with Remote Procedure Calls in a Real-Time Control System", B. Carpenter et al, Soft Prac & Exp 14(9):901-907 (Sep 1984).
P4 - Rusty Lusk lusk@anta.mcs.anl.gov. A macro/subroutine package for parallel programming, using monitors on shared memory machines, message passing on distributed memory machines. Implemented as a subroutine library for C and Fortran. An enhancement of the "Argonne macros", PARMACS. ftp://info.mcs.anl.gov/pub/p4t1.2.tar.Z info: p4@mcs.anl.gov
PABC - Intermediate language recognized by the Parallel ABC machine, used in the implementation of Concurrent Clean. "The PABC Simulator", E.G.J.M.H. NM-^Zecker, TR 89-19, U Nijmegen 1989.
我只需要提取每种语言的名称,避免其他所有内容,并考虑到某些名称有多个单词,所以我尝试使用“-”作为分隔符。但是我找不到如何正确地做到这一点。 首先我尝试了:
awk '{ print }' RS="\n\n" ORS= language.TXT
或awk '{ print }' RS= ORS="\n\n" language.TXT
但唯一的输出是文件的第一个单词:
The
我也做了:
$ awk -F "-" '{ print }' language.TXT
它确实给了我每个名字,但由于它也包含描述的每一行,它输出的内容类似于(与上面的示例相比):
+ System", B. Carpenter et al, Soft Prac & Exp 14(9):901 P4 parallel programming, using monitors on shared memory machines, message passing on distributed memory machines. Implemented as a subroutine library for C and Fortran. An enhancement of the "Argonne macros", PARMACS. ftp://info.mcs.anl.gov/pub/p4t1.2.tar.Z info: p4@mcs.anl.gov PABC in the implementation of Concurrent Clean. "The PABC Simulator", E.G.J.M.H. NM
使用 awk 的“paragrah 模式”执行此操作的正确方法是什么?
请注意,我正在使用 gawk
Using any awk in any shell on every Unix box, this is how to use awks paragraph mode:
$ awk -v RS= '{print }' file
P+
P4
PABC
以上假设您想要输出的字符串中 none 可以包含空格,因为您没有在样本输入中包含任何空格如果这些字符串可以包含空格,那么这可能就是您需要的如果它们不能包含 <blank>-
:
$ awk -v RS= -F'(^|\n) *| +-' '{print }' file
P+
P4
PABC
如果它们可以包含 <blank>-
那么您需要告诉我们如何在输入中识别它们。