方法一:表

方法一:表

AWK:将可变宽度列字段显示为 Unix 中固定间距的列字段格式。

$ cat temp.txt
QUEUE(XYZ1.REQ.YAM.ALIAS) TYPE(QCLUSTER) CLUSTER(MYCLUS) CLUSQMGR(BLAHBLAH) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)
QUEUE(XYZ4.REPL.YAM) TYPE(QCLUSTER) CLUSTER(MYSTER) CLUSQMGR(BLAHBLAHBLAHBLAH) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)
QUEUE(XYZ8.REQ.YAM) TYPE(QCLUSTER) CLUSTER(MYCTER) CLUSQMGR(BLAHBLAH) CLUSQT(QALIAS) DEFPSIST(NO) PUT(DISABLED)
QUEUE(XYZ8.REPLY.YAM) TYPE(QCLUSTER) CLUSTER( ) CLUSQMGR(ABCD) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)
QUEUE(KK.RAMAN.K.LQ) TYPE(QCLUSTER) CLUSTER(MYCLUSTER) CLUSQMGR() CLUSQT(QLOCAL) DEFPSIST(NO) PUT(ENABLED)
QUEUE(KK.RAMAN.KATHPALIA) TYPE(QREMOTE) CLUSTER(MYCLUSTER) CLUSQMGR(ABCD) CLUSQT(QLOCAL) DEFPSIST(NO) PUT(ENABLED)
QUEUE(KATHPLAIA.RAMAN) TYPE( ) CLUSTER( ) CLUSQMGR(ABCD) CLUSQT(QLOCAL) DEFPSIST(NO) PUT(ENABLED)
QUEUE(XYZ8.REQ.EQUAL.LQ) TYPE(QCLUSTER) CLUSTER(MYCLUSTER) CLUSQMGR(BLAHBLAHBLAHBLAH) CLUSQT(QLOCAL) DEFPSIST(YES) PUT(ENABLED)
QUEUE(XYZ9.RAMAN.EQUAL.LQ) TYPE(QL) CLUSTER(MYCLUSTER) CLUSQMGR(ABCD) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)
QUEUE(XX10.REPL.EQUAL.ALIAS) TYPE(QA) CLUSTER(YOURC) CLUSQMGR(ABCD) CLUSQT(QALIAS) DEFPSIST(YES) PUT(DISABLED)
QUEUE(XX10.KATHPLAIA.EQUAL.LOCAL) TYPE(LOCALQ) CLUSTER(MYCLUSTER) CLUSQMGR(BLAHBLAHBLAHBLAH) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)
QUEUE(XX11.RAMAN.EQUAL.LOCAL) TYPE(QCLUSTER) CLUSTER(MYCLUS) CLUSQMGR(BLAHBLAH) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)
QUEUE(XX11.REQ.LOCAL) TYPE(QCLUSTER) CLUSTER(MYCLUSTER) CLUSQMGR(ABCD) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)
QUEUE(RAMAN_KATHPLIA_000_11.REQ.EQUAL.REMOTE.QUEUE) TYPE(QCLUSTER) CLUSTER(MYCLUS) CLUSQMGR(BLAHBLAHBLAHBLAH) CLUSQT(QALIAS) DEFPSIST(YES) PUT(DISABLED)
QUEUE(XYZ2.REQ.RAMAN.REMOTE.QUEUE) TYPE(QLOCAL) CLUSTER(STER) CLUSQMGR(BLAHBLAH) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)
QUEUE(XYZ2.REQ.EQUAL.REMOTE.QUEUE) TYPE(QCLUSTER) CLUSTER( ) CLUSQMGR(BLAHBLAHBLAHBLAH) CLUSQT(QALIAS) DEFPSIST(YES) PUT(ENABLED)

预期:整齐的列显示

这可以通过“column”命令来实现:

$ cat temp.txt | column -t
QUEUE(XYZ1.REQ.YAM.ALIAS)                            TYPE(QCLUSTER)  CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XYZ4.REPL.YAM)                                 TYPE(QCLUSTER)  CLUSTER(MYSTER)     CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XYZ8.REQ.YAM)                                  TYPE(QCLUSTER)  CLUSTER(MYCTER)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS)              DEFPSIST(NO)    PUT(DISABLED)
QUEUE(XYZ8.REPLY.YAM)                                TYPE(QCLUSTER)  CLUSTER(            )                           CLUSQMGR(ABCD)              CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(KK.RAMAN.K.LQ)                                 TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)  CLUSQMGR()                  CLUSQT(QLOCAL)              DEFPSIST(NO)    PUT(ENABLED)
QUEUE(KK.RAMAN.KATHPALIA)                            TYPE(QREMOTE)   CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QLOCAL)              DEFPSIST(NO)    PUT(ENABLED)
QUEUE(KATHPLAIA.RAMAN)                               TYPE(           )                   CLUSTER(                    )                           CLUSQMGR(ABCD)  CLUSQT(QLOCAL)  DEFPSIST(NO)  PUT(ENABLED)
QUEUE(XYZ8.REQ.EQUAL.LQ)                             TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)  CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QLOCAL)              DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XYZ9.RAMAN.EQUAL.LQ)                           TYPE(QL)        CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XX10.REPL.EQUAL.ALIAS)                         TYPE(QA)        CLUSTER(YOURC)      CLUSQMGR(ABCD)              CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(DISABLED)
QUEUE(XX10.KATHPLAIA.EQUAL.LOCAL)                    TYPE(LOCALQ)    CLUSTER(MYCLUSTER)  CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XX11.RAMAN.EQUAL.LOCAL)                        TYPE(QCLUSTER)  CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XX11.REQ.LOCAL)                                TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(ENABLED)
QUEUE(RAMAN_KATHPLIA_000_11.REQ.EQUAL.REMOTE.QUEUE)  TYPE(QCLUSTER)  CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(DISABLED)
QUEUE(XYZ2.REQ.RAMAN.REMOTE.QUEUE)                   TYPE(QLOCAL)    CLUSTER(STER)       CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS)              DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XYZ2.REQ.EQUAL.REMOTE.QUEUE)                   TYPE(QCLUSTER)  CLUSTER(            )                           CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)

问题:

  1. 某些 AIX 和 Solaris 主机没有“column”命令。所以不能通用“列”。

  2. 即使使用列:

    (a)( )扩展到( )

    (b) 在字段之间插入了过多的空间,使得很少的行可以折叠到下一行,从而弄乱格式(19 英寸显示监视器)。

问题:

  1. 使用 awk,问题 2 再次出现(或者对于几行更糟)。请看下文。有人可以建议更好的 awk 语句吗?
  2. 还有兴趣看看是否可以使用“column”命令解决问题 2?

 

$ cat temp.txt | awk '{printf "%-55s  %-15s %-20s %-35s %-15s %-15s %-15s \n", $1,$2,$3,$4,$5,$6,$7}'
QUEUE(XYZ1.REQ.YAM.ALIAS)                                TYPE(QCLUSTER)  CLUSTER(MYCLUS)      CLUSQMGR(BLAHBLAH)                  CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XYZ4.REPL.YAM)                                     TYPE(QCLUSTER)  CLUSTER(MYSTER)      CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XYZ8.REQ.YAM)                                      TYPE(QCLUSTER)  CLUSTER(MYCTER)      CLUSQMGR(BLAHBLAH)                  CLUSQT(QALIAS)  DEFPSIST(NO)    PUT(DISABLED)
QUEUE(XYZ8.REPLY.YAM)                                    TYPE(QCLUSTER)  CLUSTER(             )                                   CLUSQMGR(ABCD)  CLUSQT(QALIAS)  DEFPSIST(YES)
QUEUE(KK.RAMAN.K.LQ)                                     TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)   CLUSQMGR()                          CLUSQT(QLOCAL)  DEFPSIST(NO)    PUT(ENABLED)
QUEUE(KK.RAMAN.KATHPALIA)                                TYPE(QREMOTE)   CLUSTER(MYCLUSTER)   CLUSQMGR(ABCD)                      CLUSQT(QLOCAL)  DEFPSIST(NO)    PUT(ENABLED)
QUEUE(KATHPLAIA.RAMAN)                                   TYPE(           )                    CLUSTER(                            )               CLUSQMGR(ABCD)  CLUSQT(QLOCAL)
QUEUE(XYZ8.REQ.EQUAL.LQ)                                 TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)   CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QLOCAL)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XYZ9.RAMAN.EQUAL.LQ)                               TYPE(QL)        CLUSTER(MYCLUSTER)   CLUSQMGR(ABCD)                      CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XX10.REPL.EQUAL.ALIAS)                             TYPE(QA)        CLUSTER(YOURC)       CLUSQMGR(ABCD)                      CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(DISABLED)
QUEUE(XX10.KATHPLAIA.EQUAL.LOCAL)                        TYPE(LOCALQ)    CLUSTER(MYCLUSTER)   CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XX11.RAMAN.EQUAL.LOCAL)                            TYPE(QCLUSTER)  CLUSTER(MYCLUS)      CLUSQMGR(BLAHBLAH)                  CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XX11.REQ.LOCAL)                                    TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)   CLUSQMGR(ABCD)                      CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(RAMAN_KATHPLIA_000_11.REQ.EQUAL.REMOTE.QUEUE)      TYPE(QCLUSTER)  CLUSTER(MYCLUS)      CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(DISABLED)
QUEUE(XYZ2.REQ.RAMAN.REMOTE.QUEUE)                       TYPE(QLOCAL)    CLUSTER(STER)        CLUSQMGR(BLAHBLAH)                  CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)
QUEUE(XYZ2.REQ.EQUAL.REMOTE.QUEUE)                       TYPE(QCLUSTER)  CLUSTER(             )                                   CLUSQMGR(BLAHBLAHBLAHBLAH) CLUSQT(QALIAS)  DEFPSIST(YES)

字段信息:

所有字段都是绑定的,并且不会扩展超过一定长度。

- Max Width field 1 = 55
- Max Width field 2 = 15
- Max Width field 3 = 20
- Max Width field 4 = 30
- Max Width field 5 = 15
- Max Width field 6 = 15
- Max Width field 7 = 15

局限性:

我想优化组织中最小尺寸显示器的显示 == 19 英寸

因此,我想将列之间的间隙最小化为单个空间。可能是方格列(如 MS Excel)

答案1

( )我只是在处理之前替换掉麻烦的部分:

sed 's/( )/()/g' temp.txt | awk '{printf "%-55s  %-15s %-20s %-35s %-15s %-15s %-15s \n", $1,$2,$3,$4,$5,$6,$7}'

如果空格数不同,请使用

sed 's/( \+)/()/g'

反而。

答案2

GNU 版本awk支持(未记录且经常未被注意到)处理开箱即用的固定长度列。使用FIELDWIDTHS变量指定每个字段的长度,然后像往常一样选择具有位置变量的字段。

例如:

$ awk 'BEGIN { FIELDWIDTHS="16 12 12 12 1"}{print $1 $2 $3 $4 $5}' cars.dat

其他方式:

$ awk -v FIELDWIDTHS="16 12 12 12 1" '{print $1 $2 $3 $4 $5}' cars.dat

如果你想使用外部变量,你可以使用:

FIELDS="10 1 3 3 15 15 15 15 22 8 100 30 20 2 1 10 10 3"
cat file | awk -v FIELDWIDTHS="${FIELDS}" '{print $1,$2,$3}'

它节省了我很多时间。

希望有帮助。

答案3

一种简单的解决方案是使用)作为 的字段分隔符awk。这就解决了你提到的两个问题。但是,这也会从每行中删除),因此您需要在调用时将它们添加回来printf

$ awk -F')' '{printf "%-55s  %-15s %-20s %-35s %-15s %-15s %-15s \n",
                      $1")",$2")",$3")",$4")",$5")",$6")",$7")"}' temp.txt 
QUEUE(XYZ1.REQ.YAM.ALIAS)                                 TYPE(QCLUSTER)  CLUSTER(MYCLUS)      CLUSQMGR(BLAHBLAH)                  CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(XYZ4.REPL.YAM)                                      TYPE(QCLUSTER)  CLUSTER(MYSTER)      CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(XYZ8.REQ.YAM)                                       TYPE(QCLUSTER)  CLUSTER(MYCTER)      CLUSQMGR(BLAHBLAH)                  CLUSQT(QALIAS)  DEFPSIST(NO)    PUT(DISABLED)  
QUEUE(XYZ8.REPLY.YAM)                                     TYPE(QCLUSTER)  CLUSTER( )           CLUSQMGR(ABCD)                      CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(KK.RAMAN.K.LQ)                                      TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)   CLUSQMGR()                          CLUSQT(QLOCAL)  DEFPSIST(NO)    PUT(ENABLED)   
QUEUE(KK.RAMAN.KATHPALIA)                                 TYPE(QREMOTE)   CLUSTER(MYCLUSTER)   CLUSQMGR(ABCD)                      CLUSQT(QLOCAL)  DEFPSIST(NO)    PUT(ENABLED)   
QUEUE(KATHPLAIA.RAMAN)                                    TYPE( )         CLUSTER( )           CLUSQMGR(ABCD)                      CLUSQT(QLOCAL)  DEFPSIST(NO)    PUT(ENABLED)   
QUEUE(XYZ8.REQ.EQUAL.LQ)                                  TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)   CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QLOCAL)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(XYZ9.RAMAN.EQUAL.LQ)                                TYPE(QL)        CLUSTER(MYCLUSTER)   CLUSQMGR(ABCD)                      CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(XX10.REPL.EQUAL.ALIAS)                              TYPE(QA)        CLUSTER(YOURC)       CLUSQMGR(ABCD)                      CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(DISABLED)  
QUEUE(XX10.KATHPLAIA.EQUAL.LOCAL)                         TYPE(LOCALQ)    CLUSTER(MYCLUSTER)   CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(XX11.RAMAN.EQUAL.LOCAL)                             TYPE(QCLUSTER)  CLUSTER(MYCLUS)      CLUSQMGR(BLAHBLAH)                  CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(XX11.REQ.LOCAL)                                     TYPE(QCLUSTER)  CLUSTER(MYCLUSTER)   CLUSQMGR(ABCD)                      CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(RAMAN_KATHPLIA_000_11.REQ.EQUAL.REMOTE.QUEUE)       TYPE(QCLUSTER)  CLUSTER(MYCLUS)      CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(DISABLED)  
QUEUE(XYZ2.REQ.RAMAN.REMOTE.QUEUE)                        TYPE(QLOCAL)    CLUSTER(STER)        CLUSQMGR(BLAHBLAH)                  CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   
QUEUE(XYZ2.REQ.EQUAL.REMOTE.QUEUE)                        TYPE(QCLUSTER)  CLUSTER( )           CLUSQMGR(BLAHBLAHBLAHBLAH)          CLUSQT(QALIAS)  DEFPSIST(YES)   PUT(ENABLED)   

我不明白为什么你有那些额外的空间。为什么不是这样的:

$ awk -F')' '{printf "%-51s%-15s%-20s%-28s%-15s%-15s%-15s\n",
                      $1")",$2")",$3")",$4")",$5")",$6")",$7")"}' temp.txt 
QUEUE(XYZ1.REQ.YAM.ALIAS)                           TYPE(QCLUSTER) CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(XYZ4.REPL.YAM)                                TYPE(QCLUSTER) CLUSTER(MYSTER)     CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(XYZ8.REQ.YAM)                                 TYPE(QCLUSTER) CLUSTER(MYCTER)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS) DEFPSIST(NO)   PUT(DISABLED) 
QUEUE(XYZ8.REPLY.YAM)                               TYPE(QCLUSTER) CLUSTER( )          CLUSQMGR(ABCD)              CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(KK.RAMAN.K.LQ)                                TYPE(QCLUSTER) CLUSTER(MYCLUSTER)  CLUSQMGR()                  CLUSQT(QLOCAL) DEFPSIST(NO)   PUT(ENABLED)  
QUEUE(KK.RAMAN.KATHPALIA)                           TYPE(QREMOTE)  CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QLOCAL) DEFPSIST(NO)   PUT(ENABLED)  
QUEUE(KATHPLAIA.RAMAN)                              TYPE( )        CLUSTER( )          CLUSQMGR(ABCD)              CLUSQT(QLOCAL) DEFPSIST(NO)   PUT(ENABLED)  
QUEUE(XYZ8.REQ.EQUAL.LQ)                            TYPE(QCLUSTER) CLUSTER(MYCLUSTER)  CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QLOCAL) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(XYZ9.RAMAN.EQUAL.LQ)                          TYPE(QL)       CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(XX10.REPL.EQUAL.ALIAS)                        TYPE(QA)       CLUSTER(YOURC)      CLUSQMGR(ABCD)              CLUSQT(QALIAS) DEFPSIST(YES)  PUT(DISABLED) 
QUEUE(XX10.KATHPLAIA.EQUAL.LOCAL)                   TYPE(LOCALQ)   CLUSTER(MYCLUSTER)  CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(XX11.RAMAN.EQUAL.LOCAL)                       TYPE(QCLUSTER) CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(XX11.REQ.LOCAL)                               TYPE(QCLUSTER) CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(RAMAN_KATHPLIA_000_11.REQ.EQUAL.REMOTE.QUEUE) TYPE(QCLUSTER) CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS) DEFPSIST(YES)  PUT(DISABLED) 
QUEUE(XYZ2.REQ.RAMAN.REMOTE.QUEUE)                  TYPE(QLOCAL)   CLUSTER(STER)       CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  
QUEUE(XYZ2.REQ.EQUAL.REMOTE.QUEUE)                  TYPE(QCLUSTER) CLUSTER( )          CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)  

另一种方法是将 a 后面的所有空格转换)为制表符,并使用制表符作为字段分隔符:

$ sed 's/)  */)\t/g' temp.txt | 
    awk -F'\t' '{printf "%-52s%-15s%-20s%-28s%-15s%-15s%-15s\n",
                         $1,$2,$3,$4,$5,$6,$7}'
QUEUE(XYZ1.REQ.YAM.ALIAS)                           TYPE(QCLUSTER) CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(XYZ4.REPL.YAM)                                TYPE(QCLUSTER) CLUSTER(MYSTER)     CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(XYZ8.REQ.YAM)                                 TYPE(QCLUSTER) CLUSTER(MYCTER)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS) DEFPSIST(NO)   PUT(DISABLED)  
QUEUE(XYZ8.REPLY.YAM)                               TYPE(QCLUSTER) CLUSTER( )          CLUSQMGR(ABCD)              CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(KK.RAMAN.K.LQ)                                TYPE(QCLUSTER) CLUSTER(MYCLUSTER)  CLUSQMGR()                  CLUSQT(QLOCAL) DEFPSIST(NO)   PUT(ENABLED)   
QUEUE(KK.RAMAN.KATHPALIA)                           TYPE(QREMOTE)  CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QLOCAL) DEFPSIST(NO)   PUT(ENABLED)   
QUEUE(KATHPLAIA.RAMAN)                              TYPE( )        CLUSTER( )          CLUSQMGR(ABCD)              CLUSQT(QLOCAL) DEFPSIST(NO)   PUT(ENABLED)   
QUEUE(XYZ8.REQ.EQUAL.LQ)                            TYPE(QCLUSTER) CLUSTER(MYCLUSTER)  CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QLOCAL) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(XYZ9.RAMAN.EQUAL.LQ)                          TYPE(QL)       CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(XX10.REPL.EQUAL.ALIAS)                        TYPE(QA)       CLUSTER(YOURC)      CLUSQMGR(ABCD)              CLUSQT(QALIAS) DEFPSIST(YES)  PUT(DISABLED)  
QUEUE(XX10.KATHPLAIA.EQUAL.LOCAL)                   TYPE(LOCALQ)   CLUSTER(MYCLUSTER)  CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(XX11.RAMAN.EQUAL.LOCAL)                       TYPE(QCLUSTER) CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(XX11.REQ.LOCAL)                               TYPE(QCLUSTER) CLUSTER(MYCLUSTER)  CLUSQMGR(ABCD)              CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(RAMAN_KATHPLIA_000_11.REQ.EQUAL.REMOTE.QUEUE) TYPE(QCLUSTER) CLUSTER(MYCLUS)     CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS) DEFPSIST(YES)  PUT(DISABLED)  
QUEUE(XYZ2.REQ.RAMAN.REMOTE.QUEUE)                  TYPE(QLOCAL)   CLUSTER(STER)       CLUSQMGR(BLAHBLAH)          CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   
QUEUE(XYZ2.REQ.EQUAL.REMOTE.QUEUE)                  TYPE(QCLUSTER) CLUSTER( )          CLUSQMGR(BLAHBLAHBLAHBLAH)  CLUSQT(QALIAS) DEFPSIST(YES)  PUT(ENABLED)   

答案4

方法一:表

假设 GNU sed。 Sedtbl即时生成命令,然后tbl运行以生成所需的输出。

sed -e '
   1i\
.TS\
tab( );

   1{
      h;s/(\s\+)/(,)/g;
      s/\s\+$//;s/^\s\+//;s/\s\+/ /g;
      s/\S\+//g
      s/$/ /;s/ /l&/g;s/.$/./
      G;b
   }
   s/(\s\+)/(,)/g
   $a\
.TE
' | tbl - | nroff -Tascii -ms | sed '/./!d; s/(,)/( )/g'

解释

在我们的例子中,sed 部分不必要地复杂。我写的目的是让它不依赖于数据中有多少列,并查看手头的数据来得出 tbl 语法。因为在我们的例子中,我们确定正好有 7 列,所以 sed 代码可以写得更简单(这就是 sed 逻辑实际上生成的内容,这 7 个空格分隔的 ell 和一个尾随点):

sed -e '
   1i\
.TS\
tab( );\
l l l l l l l.
   s/(\s\+)/(,)/g
   $a\
.TE
'

tbl 的基本样板可归结为以下内容:

  1. 表开头的强制 .TS
  2. 使用 tab(delim_char) 的数据分隔符;
  3. 列式发布:l -> 左对齐、r-> 右对齐、c-> 中心对齐和 n 数字列。它们的数量必须与数据列匹配。
  4. 表尾强制 .TE
  5. 将 tbl 的 o/p 传递给 nroff,这就是该实用程序的全部内容。

方法二:Perl

此方法确定每个字段的最大宽度,然后使用此信息动态生成 printf 格式说明符。此方法将给出最短的间距。

perl -lne '
   tr/\t/ /;
   s/\((\s+)\)/"(" . "+" x length($1) . ")"/eg;
   ($a, @F) = (-1, split);
   s/\((\++)\)/"(" . " " x length($1) . ")"/eg for @F;
   push @A, [@F];
   $a++, length > $maxW[$a] and $maxW[$a] = length for @F;
   END {
      my $fmt = join $", map { "%-${_}s" } @maxW;
      print sprintf $fmt, @$_ for @A;
   }
' temp.txt

相关内容