将多条线变成单线

将多条线变成单线

我有以下 4 行:

EXTRACT    ETEST  Last Started 2016-03-20 20:56   Status RUNNING  
Checkpoint Lag       00:00:02 (updated 00:00:04 ago)
EXTRACT    PTEST  Initialized   2016-03-31 06:25   Status STOPPED 
Checkpoint Lag       00:00:00 (updated 02:21:09 ago)

我只想将它们分成两行,如下所示:

EXTRACT    ETEST  Last Started 2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:04 ago)
EXTRACT    PTEST  Initialized  2016-03-31 06:25   Status STOPPED Checkpoint Lag       00:00:00 (updated 02:21:09 ago)

我可以用以下方法制作cat /tmp/glog.lst|xargs -n 14

EXTRACT    ETEST  Last Started 2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:04 ago)
EXTRACT    PTEST  Initialized  2016-03-31 06:25   Status STOPPED Checkpoint Lag       00:00:00 (updated 02:21:09 ago)

但是row1有14个字段,row2有13个字段。

问题是 row2 已初始化与“上次启动”

我想制作一行少于 14 列到 14 列的行,安装带有或“N”(或)某些内容的虚拟列,这样它将有 14 列,这样我就可以轻松处理我的脚本。

我想最终做到

EXTRACT    ETEST  Last Started  2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:04 ago)
EXTRACT    PTEST  Initialized N 2016-03-31 06:25   Status STOPPED Checkpoint Lag       00:00:00 (updated 02:21:09 ago)

这就是我需要的

$ awk '{ if (FNR%2==1) tmp=$0; else print tmp,$0 }'  /tmp/gglog.lst
EXTRACT    ETEST1  Initialized 2016-03-20 20:56   Status STOPPED Checkpoint Lag       00:00:02 (updated 00:00:09 ago)
EXTRACT    ETEST2  Last Started 2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:04 ago)
EXTRACT    ETEST3  Last Started 2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:09 ago)
EXTRACT    PTEST1  Last Started 2016-03-29 08:46   Status RUNNING Checkpoint Lag       00:00:00 (updated 00:00:02 ago)
EXTRACT    PTEST0  Last Started 2016-03-29 08:46   Status RUNNING Checkpoint Lag       00:00:00 (updated 00:00:05 ago)

现在我想取出Initialized/Last/Started的列值,这样它就会这样

EXTRACT    ETEST1  2016-03-20 20:56   Status STOPPED Checkpoint Lag       00:00:02 (updated 00:00:09 ago)
EXTRACT    ETEST2  2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:04 ago)
EXTRACT    ETEST3  2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:09 ago)
EXTRACT    PTEST1  2016-03-29 08:46   Status RUNNING Checkpoint Lag       00:00:00 (updated 00:00:02 ago)
EXTRACT    PTEST0  2016-03-29 08:46   Status RUNNING Checkpoint Lag       00:00:00 (updated 00:00:05 ago)

或者我们可以将“初始化”中断为“立即初始化”吗?无论我们归档的哪一行都已初始化,我们希望将其分解为“Init Now”

EXTRACT    ETEST1  Init Now 2016-03-20 20:56   Status STOPPED Checkpoint Lag       00:00:02 (updated 00:00:09 ago)
EXTRACT    ETEST2  Last Started 2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:04 ago)
EXTRACT    ETEST3  Last Started 2016-03-20 20:56   Status RUNNING Checkpoint Lag       00:00:02 (updated 00:00:09 ago)
EXTRACT    PTEST1  Last Started 2016-03-29 08:46   Status RUNNING Checkpoint Lag       00:00:00 (updated 00:00:02 ago)
EXTRACT    PTEST0  Last Started 2016-03-29 08:46   Status RUNNING Checkpoint Lag       00:00:00 (updated 00:00:05 ago)

答案1

  • 要两两合并行:

    awk '{ if(FNR%2==1) tmp=$0; else print tmp,$0 }' tmp/glog.lst

  • 替换InitializedInit Now

    sed 's/Initialized/Init Now/' tmp/glog.lst

  • 要同时执行这两项操作,请使用管道将它们链接起来:

    awk '{ if(FNR%2==1) tmp=$0; else print tmp,$0 }' tmp/glog.lst | sed 's/Initialized/Init Now/'

答案2

要回答您的标题问题,您可以使用以下命令连接连续行sed

sed 'N;s/\n/ /' /tmp/glog.lst

但是,您的专栏问题实际上完全是一个单独的问题。这最好的解决方案是,逆流而上glog.lst到生成文件并制作它的工具使用一些合理的分隔符而不是空白。

如果这是完全不可能的,你也许可以解决它,但其他任何事情都必然只是一个黑客。

相关内容