6

AWK: Display variable width columns fields into fixed spaced Column fields Format in 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)

Expected: A neat column display

This can be achieved by "column" command:

$ 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)

Problems:

  1. Certain AIX and Solaris hosts doesn't have "column" command. So can't use "column" universally.

  2. Even with usage of column:

    (a) ( ) expands to ( )

    (b) More space than necessary is inserted between fields making few rows to fold into next line thus messing up formatting (19 Inch display monitor).

Questions:

  1. Using awk, Problem 2 re-surfaces (or worse for few lines). Please see below. Can someone suggest a better awk statement ?
  2. Also interested to see if Problem 2 can be resolved using "column" command ?

 

$ 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)

Field info:

All fields are bound and don't expand beyond certain length.

- 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

Limitation:

I want to optimize the display for least sized monitor in organisation == 19 Inches

So, I want to minimize the gap between columns to a single space. Possibly, checkered columns (like MS Excel)

7

I would just replace the troublesome ( ) before processing:

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

If the number of spaces varies, use

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

instead.

  • Although gaps in ( ) are absent in temp.txt but introduced by awk statement. I inserted sed at the end and quickly ran into another problem. Gaps introduced by awk are variable in length. This temp.txt is essentially an app generated data in real time that I would be feeding to awk; and I wouldn't know how many space variation in gap exists. :( – Raman Kathpalia Mar 17 '17 at 19:38
  • Your initial cat temp.txt shows the spaces between the brackets. – Stephen Kitt Mar 17 '17 at 19:43
  • Is it OK if all "()" pairs are replaced with "( )" in the final output? In the input, there's a mixture of "()" and "( )". The idea behind removing the space before running AWK is so that it isn't taken into account when splitting the line into fields, which should then allow consistent output (without the huge gaps between brackets). – Stephen Kitt Mar 17 '17 at 20:06
  • As space is introduced by awk,I didn't try 1st option: sed 's/( )/()/g' temp.txt | awk '{printf "%-55s %-15s %-20s %-35s %-15s %-15s %-15s \n", $1,$2,$3,$4,$5,$6,$7}' - Works sed 's/( +)/()/g' temp.txt | awk '{printf "%-55s %-15s %-20s %-35s %-15s %-15s %-15s \n", $1,$2,$3,$4,$5,$6,$7}' - Does not work cat temp.txt | awk '{printf "%-55s %-15s %-20s %-35s %-15s %-15s %-15s \n", $1,$2,$3,$4,$5,$6,$7}' | sed 's/( +)/()/g' - Does not work cat temp.txt | awk '{printf "%-55s %-15s %-20s %-35s %-15s %-15s %-15s \n", $1,$2,$3,$4,$5,$6,$7}' | sed 's/( )/()/g' - Does not work – Raman Kathpalia Mar 17 '17 at 20:16
  • Sorry for bad formatted response, I'm not sure how to fix response above. – Raman Kathpalia Mar 17 '17 at 20:19
4

One easy solution would be to use the ) as the field separator for awk. That gets around both of the issues you mention. However, that also removes the ) from each line, so you need to add them back when you call 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)   

I don't understand why you have those extra spaces though. Why not something like this:

$ 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)  

Another approach is to convert all spaces following a ) to tabs and use tabs as the field separator:

$ 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)   
  • Thanks for a simple solution. I specifically like "another approach" where you've converted all spaces following a ) to tab and use that tab as a field separator. This has given me more ideas in other areas to implement. Thank you! – Raman Kathpalia Mar 20 '17 at 14:09
  • Regarding your other question around "extra spaces"; I gave Max possible length to which a field can expand should an entry arrives requiring Max field. I should have adjusted my example to reflect that. – Raman Kathpalia Mar 20 '17 at 14:31
3

You could replace ( ) with something like +++, pass it to column -t and substitute +++ back:

$ sed 's/( )\{1,\}/+++/g' temp.txt | column -t | sed 's/+++/\( \)/g' 
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)

This doesn't work for variable with whitespace if you want to keep the exact spacing between ( ).

  • Thank you for the answer. That's a good trick . Together with @Stephen Kitt and your answer, I can now use awk OR column This forum is Godsend. – Raman Kathpalia Mar 17 '17 at 20:37
3

Method-1: tbl

#Assuming GNU sed. Sed generates the tbl commands on the fly and then tbl is run to generate the desired output.

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'

Explanation

Actually the sed portion is needlessly complex in our case. I wrote to make it
not being dependent upon how many columns are there in the data & looking at the
data in hand come up with the tbl syntax. Since in our case we are sure there are
exactly 7 columns so the sed code could have been simply be written simply':
(which is what the sed logic actually was generating essentially,
 those 7  space separated ells and a trailing dot)

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

The essential boilerplate of the tbl boils down to the following:
a) Mandatory .TS for the start of table
b) data delimiters using    tab(delim_char);
c) columnar publishing: l -> left-justified, r-> right justified,
    c->center-justified, & n-numeric col. Their number must match
    the data cols.
e) Mandatory .TE for the end of table

f) pass on the o/p of tbl to nroff and that's all there's to this utility.

Method-2: Perl

This method determines the maximum widths per field and then using this info generates the printf format specifier dynamically. This method will give the shortest spacing.

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
  • 1
    These are both good solutions (and +1), but pretty obscure for those not familiar with the tools you're using. An explanation would be very helpful. – terdon Mar 17 '17 at 22:20
  • @terdon I have added a brief explanation . Thanks. – user218374 Mar 26 '17 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.