将用空行分段的文本中每段拼成一行
例题描述和简单分析
有文本文件 txt.txt,数据如下所示:
this is
a
single record
here is the next record followed by a line with just a space
finally
our
last
record
现在要将每段数据变成一个字符串,结果如下:
-> this is a single record
-> here is the next record followed by a line with just a space
-> finally our last record
解法及简要说明
在集算器中编写脚本 p1.dfx,如下所示:
A |
|
1 |
=file("txt.txt").import@i() |
2 |
=A1.group@i(~[-1]==null).("-> "/~.select(~).concat(" ")) |
3 |
=file("result.txt").export(A2) |
简要说明:
A1 读取 txt 数据,@i 表示结果集只有 1 列时返回成序列
A2 条件分组,条件为上一个成员为空,每组以串"->"开头,再接组内的成员用空格符拼成的串
A3 结果导出至 result.txt
https://stackoverflow.com/questions/56655536/java-streams-and-multiline-records
英文版