如何将常规字符串连接到每个子行的每个第一列

时间:2014-12-13 08:56:17

标签: bash perl awk sed

我想在下面的案例中加入每个第一个通用字符串" ADMIN"和" DB"对于他们所代表的数据以及他们每次在第一列上所采取的地方。

示例:

ADMIN
                         ADMIN_DB         Running          1          0          1          0          0          0         80
                         ADMIN_CATALOG    Running          0          0          1          0          0          0        452
                         ADMIN_CAT        Running          0          0          1          0          0          0         58
DB
               SLAVE_DB    Running          2          0          3          0          0          0         94
               DB_BAK      Running          1          0          1          0          0          0         54
             HISTORY_DB    Running          0          0          1          0          0          0         40
         HISTORY_DB_BAK    Running          0          0          1          0          0          0         59

期望:

ADMIN ADMIN_DB         Running          1          0          1          0          0          0         80
ADMIN ADMIN_CATALOG    Running          0          0          1          0          0          0        452
ADMIN ADMIN_CAT        Running          0          0          1          0          0          0         58
DB SLAVE_DB            Running          2          0          3          0          0          0         94
DB DB_BAK              Running          1          0          1          0          0          0         54
DB HISTORY_DB          Running          0          0          1          0          0          0         40
DB HISTORY_DB_BAK      Running          0          0          1          0          0          0         59

在过去,我有一个例子,这是可以做事情的起点,但我在这种脚本中并不是那么多:perl -ne 'chomp; if($. % 2){print "$_,";next;}

1 个答案:

答案 0 :(得分:2)

怎么样

awk 'NF==1{ val=$0; next} {print val" "$0}' input

您可以使用column实用程序格式化输出

$ awk 'NF==1{ val=$0; next} { print val" "$0}' input | column -t
ADMIN  ADMIN_DB        Running  1  0  1  0  0  0  80
ADMIN  ADMIN_CATALOG   Running  0  0  1  0  0  0  452
ADMIN  ADMIN_CAT       Running  0  0  1  0  0  0  58
DB     SLAVE_DB        Running  2  0  3  0  0  0  94
DB     DB_BAK          Running  1  0  1  0  0  0  54
DB     HISTORY_DB      Running  0  0  1  0  0  0  40
DB     HISTORY_DB_BAK  Running  0  0  1  0  0  0  59