Search code examples
bashawkkshaix

How to align the nth column that has multiple lines


I am having 3 variables that I use printf to align their outputs I know the first env var is one line so the 2nd var as well The problem with the 3rd var is that this variable has huge output hence I would like to list them aligned ( i hope below would clear my question )

** I don't have column command **

I am using

printf "%-20s %-5s %-20s \n" "${OUT}" "${COUNT}" "${DATA}"


OUT11                OUT2  OUT3
----------           ----- --------------
GATE1                14    CU4 CU2 CU9var CU3 CU1 CU11admin CU10opt not_sy 
CU_doonce cutttCU clocal_CU global_CU tivoli_CU 
GATE2                70    gdba_CU cdba_CU vudb_CU tti1_CU tti3_CU tt3_CU
3c2_CU tt3c3_CU tt3c4_CU tt3d1_CU tt3a1_CU tt3u1
3t3_CU tt3t4_CU tt4_CU utt4_CU tt4c1_CU tt4c2_CU
4d1_CU tt4a1_CU tt4u1_CU tt4t1_CU tt4t2_CU tt4t3
tt5_CU tt5c1_CU tt5c2_CU tt5c3_CU tt5c4_CU tt5d1
5t1_CU tt5t2_CU tt5t3_CU tt5t4_CU tt6_CU utt6_CU
6c3_CU tt6c4_CU tt6d1_CU tt6a1_CU tt6u1_CU tt6t1
6t4_CU tt7_CU utt7_CU tt7c1_CU tt7c2_CU tt7c3_CU
7a1_CU tt7u1_CU tt7t1_CU tt7t2_CU tt7t3_CU tt7t4

and I need them as:

OUT11                OUT2  OUT3
----------           ----- --------------
GATE1                14    CU4 CU2 CU9var CU3 CU1 CU11admin CU10opt not_sy 
                           CU_doonce cutttCU clocal_CU global_CU tivoli_CU 
GATE2                70    gdba_CU cdba_CU vudb_CU tti1_CU tti3_CU tt3_CU
                           3c2_CU tt3c3_CU tt3c4_CU tt3d1_CU tt3a1_CU tt3u1
                           3t3_CU tt3t4_CU tt4_CU utt4_CU tt4c1_CU tt4c2_CU
                           4d1_CU tt4a1_CU tt4u1_CU tt4t1_CU tt4t2_CU tt4t3
                           tt5_CU tt5c1_CU tt5c2_CU tt5c3_CU tt5c4_CU tt5d1
                           5t1_CU tt5t2_CU tt5t3_CU tt5t4_CU tt6_CU utt6_CU
                           6c3_CU tt6c4_CU tt6d1_CU tt6a1_CU tt6u1_CU tt6t1
                           6t4_CU tt7_CU utt7_CU tt7c1_CU tt7c2_CU tt7c3_CU
                           7a1_CU tt7u1_CU tt7t1_CU tt7t2_CU tt7t3_CU tt7t4


Solution

  • Could you please try following.

    your_command | awk '
    /GATE/{
      val=index($0,$3)
      while(++i<val){
        space=space OFS
      }
    }
    !/^GATE/{
      $0=space $0
    }
    1'
    

    One liner:

    your_command | awk '/GATE/{val=index($0,$3);while(++i<val){space=space OFS}} !/^GATE/{$0=space $0} 1'
    

    OR as per Ed sir's comment try following:

    your_command | awk '
    /GATE/{
      space=sprintf("%*s",index($0,$3),"")
    }
    !/^GATE/{
      $0=space $0
    }
    1'