awk - , fixed width columns

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.


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)   

The GNU version of awk supports (undocumented and frequently unnoticed) dealing with fixed length columns out of the box. Use the FIELDWIDTHS variable to specify the length of each field, then pick the fields with positional variables as usual.

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

Another way

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

If you want to use external variables you can use

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}'

It saved me lots of time.

Hope it helped.