从txt文件中提取文本



我有一个带有记录的txt文件。记录遵循以下模式:

six lines, blank space, six lines,.....就像这个例子:

string line 1
string line 2
string line 3
string line 4
string line 5 (year format yyyy)
string line 6 (can use several lines)
<blank space> (always a blank space when a new txt block begins)
string line 1
string line 2
string line 3
string line 4
string line 5 (year format yyyy)
string line 6
下面是一个合适的例子:我需要标题(第2行)和年份(第5行)
Hualong Yu, Geoffrey I. Webb,
Adaptive online extreme learning machine by regulating forgetting factor by concept drift map,
Neurocomputing,
Volume 343,
2019,
Pages 141-153,
ISSN 0925-2312,
https://doi.org/10.1016/j.neucom.2018.11.098.
https://www.sciencedirect.com/science/article/pii/S0925231219301572
Antonino Feitosa Neto, Anne M.P. Canuto,
EOCD: An ensemble optimization approach for concept drift applications,
Information Sciences,
Volume 561,
2021,
Pages 81-100,
ISSN 0020-0255,
https://doi.org/10.1016/j.ins.2021.01.051.
https://www.sciencedirect.com/science/article/pii/S002002552100089X

我想提取第2行的字符串和第5行的年份,所有文本块(以空格分隔),将其保存到另一个txt文件中,如下所示:

string line2 , yyyy

我没有linux shell的经验,所以我在这里要求一些输入来帮助我做这个任务。

感谢

如果您不关心第5行后面的逗号,只需:

awk '{print $2, $5}' RS= FS='\n' input > output

这假设分隔记录的空行确实是完全空白的,并且不包含任何空格。如果该行中有空格,则需要对数据进行预过滤以删除它。

,

$ cat input
Hualong Yu, Geoffrey I. Webb,
Adaptive online extreme learning machine by regulating forgetting factor by concept drift map,
Neurocomputing,
Volume 343,
2019,
Pages 141-153,
ISSN 0925-2312,
https://doi.org/10.1016/j.neucom.2018.11.098.
https://www.sciencedirect.com/science/article/pii/S0925231219301572
Antonino Feitosa Neto, Anne M.P. Canuto,
EOCD: An ensemble optimization approach for concept drift applications,
Information Sciences,
Volume 561,
2021,
Pages 81-100,
ISSN 0020-0255,
https://doi.org/10.1016/j.ins.2021.01.051.
https://www.sciencedirect.com/science/article/pii/S002002552100089
$ awk '{print $2, $5}' RS= FS='\n' input
Adaptive online extreme learning machine by regulating forgetting factor by concept drift map, 2019,
EOCD: An ensemble optimization approach for concept drift applications, 2021,

类似于

perl -00 -nE 'my @ln = (split /,n/)[1,4]; say join(",", @ln)'  input.txt > output.txt

应该至少作为一个起始点。一次读取一个段落,将其分成几行,并在同一行中打印出您要查找的两行,中间用逗号分隔。

相关内容

  • 没有找到相关文章

最新更新