Tabulate text using equals sign as separator

Viewed 153

in my unix script ${job_details} is variable. this variable I am passing in mailx body command to print the output in email.

Current output :

 LABEL NAME =  AA 
 APPLICATION NAME =  EST
 ONSITE LEAD =  XXX 
 OFFSHORE LEAD =  YYYY 
 TEAM DL NAME =  k 
 PROJECT MANAGER =  SSS 
 SLA =  NULL 
 CONFLUENCE LINK =  https:google.com

But I want the output in proper alignment Like

 LABEL NAME         =  AA 
 APPLICATION NAME   =  EST
 ONSITE LEAD        =  XXX 
 OFFSHORE LEAD      =  YYYY 
 TEAM DL NAME       =  k 
 PROJECT MANAGER    =  SSS 
 SLA                =  NULL 
 CONFLUENCE LINK    =  https:google.com

I tried with echo ${job_details}|column -t that would not helps me. creating spaces and adding the tab space in first name and last name

2 Answers

Just change input/output separators to equals sign.

$ column -t -s= -o= <<< "${job_details}"
 LABEL NAME       =  AA
 APPLICATION NAME =  EST
 ONSITE LEAD      =  XXX
 OFFSHORE LEAD    =  YYYY
 TEAM DL NAME     =  k
 PROJECT MANAGER  =  SSS
 SLA              =  NULL
 CONFLUENCE LINK  =  https:google.com

It's also possible to have the same result using AWK, this is another way for which it interests.

echo "$job_details" | awk '
{
    gsub(/^[ ]/, "", $0)
    tab[NR]=$0
}
END{
    for(x=1; x <= NR; x++){
        split(tab[x],a,"=")
        num=split(a[1],b,"")

        if(c_lent < num){
            c_lent=num
        }
    }
    for(y=1; y <= NR; y++){
        split(tab[y],c,"=")
        printf "%*-s=%s\n",c_lent, c[1],c[2]
    }
}' 

Explanation:

First I create an array in which I put all the '$0' entries, (NR) allows to index the array dynamically. In the END section, I calculate the longest string of the first column, then I read the array and display the result using the command 'printf'.

Related