使用公共字符串“Message”按特定顺序拆分文本

smc*_*smc 3 scripting sed awk shell-script tr

我有一个包含以下内容的文本文件

$ cat foo.txt

some text 
email@id.com
8903457923
2018-02-09 07:12 (Asia/Kolkata)
again some text over here
some more text again
Message
some text 
email@id.com
8903457923
2018-02-05 07:12 (Asia/Kolkata)
again some text over here
some more text again
Message
Run Code Online (Sandbox Code Playgroud)

我想获得以下输出

$ cat foo.txt

some text  email@id.com  8903457923  2018-02-09 07:12 (Asia/Kolkata)  again some text over her  some more text again  Message

some text email@id.com  8903457923  2018-02-05 07:12 (Asia/Kolkata) again some text over here  some more text again  Message
Run Code Online (Sandbox Code Playgroud)

我想我可以使用 tr 并将“消息”作为通用字符串来实现。但不确定如何实现这一点。

Jef*_*ler 6

如果当前行不是“Message”,则将该行追加到列表中,加入OFS;当您看到“消息”时,打印当前列表(由 OFS 与当前的“消息”行连接):

awk '/^Message$/ { print t OFS $0 ORS; t=""; } !/^Message$/ { t=(t ? t OFS $0 : $0) }' < foo.txt
Run Code Online (Sandbox Code Playgroud)

t=(t ? t OFS $0 : $0)部分是一个三元运算符;它检查是否t为空;如果是,则将当前行分配给它;否则,用 OFS 附加当前值,后跟当前行。

输出:

some text  email@id.com 8903457923 2018-02-09 07:12 (Asia/Kolkata) again some text over here some more text again Message

some text  email@id.com 8903457923 2018-02-05 07:12 (Asia/Kolkata) again some text over here some more text again Message
Run Code Online (Sandbox Code Playgroud)


paw*_*318 5

使用 AWK 更简单的方法:

awk 'BEGIN { ORS=RS="Message\n" } gsub("\n"," ")' ./in.txt