I have written a script which displays Name and department of the user and that works.Moreover i am trying to draw a divider line with Name title assign to the columns as they needed. I am trying to do it with printf but somehow not able to get it as of now.. any help will be appreciated..

below is the script and expected result..

for names in `cat namefile`;


DATA1="`/grid/common/bin/cod -u $names | awk -F: '/Department/ {print $2}'`"
DATA2="`/grid/common/bin/cod -u $names | awk -F: '/Name/ {print $2}'`"

#printf '%*s\n' "${COLUMN'S:-$(tput cols)}" '' | tr ' ' -
printf "%-50s : %10s\n" "$DATA2" "$DATA1"
bash-4.1$ sh Userdetail.sh
        Karn Kumar                                 :   TT, Infra, VM & India
        Manas .                                    :   TT, Infra, VM & India
        Pranjal Agrawal                            :   TT, Infra, VM & India
        Rogen Mana                                 :   PP OPS-Brazil

I'm trying to find a way draw a line with Titles before the output will display like below expected:

  Name                                         Department


Karn Kumar                                 :   TT, Infra, VM & India
Manas .                                    :   TT, Infra, VM & India
Pranjal Agrawal                            :   TT, Infra, VM & India
Rogen Mana                                 :   PP OPS-Brazil

Just for more how to know on the command part..

bash-4.1$ /grid/common/bin/cod -u karn
        Name:        Karn Kumar
        Title:       IT -Staff Systems Engineer

        Department:  TT, Infra, VM & India
        Mgr:         KK LIN
        Mgr Login:   ttlin
        E-Mail:      [email protected]
        Phone:       09999999999
        Internal #:  
        Ext:         4848
        Cell:        8777880559
        Fax:            -
        Building:    NOIDA 03
        Floor:       03
        Room:        3.3.109
  • Also, side comment: please avoid using the backtick for command substitution use $( command ) instead. It makes it much easier to read and avoids certain problems. In addition , don't use for variable in $(cat file.txt). There are other, better ways to read a file line-by-line. See mywiki.wooledge.org/BashFAQ/001 Jan 19, 2017 at 4:03

1 Answer 1


You have printf "%-50s : %10s\n" "$DATA2" "$DATA1" , which tells me in total you will have 63-character wide line (including spaces and :). Thus we will want to printf "%63s" string. Simple approach is to print spaces and then convert them all to - like so

printf "%60s" " " | tr ' ' '-' 

We certainly could use simpler approach, but it is specific to bash. We can, however, modify it slightly:

printf "=%.0s"  $(seq 1 63)

This will work in any shell and system that has seq installed.

Thus, print the header before going into the loop. For example, here's very very simple case:

$ cat ./print_header.sh                                                                                           
# print header
printf "%-50s : %10s\n" "Name"  "Department"
# print separator
printf "=%.0s"  $(seq 1 63)
# insert a newline
printf "\n"
# and this is where your for loop would begin.
# just for the sake of example, there's only one line
printf "%-50s : %10s\n" "John Doe"  "IT,Infra"
$ ./print_header.sh                                                                                               
Name                                               : Department
John Doe                                           :   IT,Infra
  • thanks for the trick , but how to fit that into the expected results .
    – krock1516
    Jan 19, 2017 at 4:29
  • using this , it adds a "-" line before every output display'd.
    – krock1516
    Jan 19, 2017 at 4:32
  • @Karn I'll add a simple example. I don't have the input file that you're using, so I'll just make something up Jan 19, 2017 at 4:35
  • 1
    @Karn answer edited Jan 19, 2017 at 4:44
  • i see that, i have also edited the command output which i'm parsing the desired output and having them into DATA1 & DATA2 vars.
    – krock1516
    Jan 19, 2017 at 4:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .