如何将多个sed输出行(-e)连接到一行

时间:2015-01-22 15:44:22

标签: regex bash sed

我使用sed从日志文件中提取一些数据。我的命令如下:

sed -n -e 's/.*\(date\).*/\1/p' -e 's/.*\(details in that entry\).*/\1/p'-e 's/.*\(some more details\).*/\1/p'

这总是产生2行或更多行:

date
some details
some more details
date(2nd entry) 
some details ( 2nd entry ) 
some more details (2nd entry) 

依旧......

如何将来自sed命令集的行直接加入到这样的一行?

date some details some more details
date some details some more details....

1 个答案:

答案 0 :(得分:1)

它的第3行:

awk 'ORS=NR%3?" ":RS' file
date some details some more details
date some details some more details

paste -d " " - - - < file
date some details some more details
date some details some more details

如果您可以使用date作为触发词,则可以使用此gnu awk

awk 'NR>1 {$1=RS FS $1;print}' RS="date" file
date some details some more details
date some details some more details

(由gnu awk中的多个字符组成的RS