将不同行上的单词合并为一行

M.A*_*A.G 3 linux grep awk text-processing

我有一个文件,其中包含彼此下的单词列表,其中这些单词属于一个句子,然后属于下一个句子的单词也彼此在下。与一个句子相关的单词块后跟一个空格,如下面的表示#2 所示

预期输出:(表示#1):

These are the words for sentence 1
These are the words for sentence 2
Run Code Online (Sandbox Code Playgroud)

预期输入:(表示#2):

These
are
the
words
for
sentence 1

these
are
the
words
for
sentence 2
Run Code Online (Sandbox Code Playgroud)

我尝试遵循这个问题,但是当我对不同的句子使用不同的单词时,它不起作用,那么如何在linux中将表示编号2更改为表示编号1?

Ed *_*ton 7

$ awk -v RS= '{$1=$1}1' file
These are the words for sentence 1
these are the words for sentence 2
Run Code Online (Sandbox Code Playgroud)


Ark*_*zyk 5

使用 awk:

awk 'BEGIN { RS = "" } {gsub(/ *\n */, " "); print}' FILE
Run Code Online (Sandbox Code Playgroud)