0

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..

#!/bin/sh
for names in `cat namefile`;

do

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"
done
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:      karn@abc.com
        Phone:       09999999999
        Internal #:  
        Ext:         4848
        Cell:        8777880559
        Fax:            -
        Building:    NOIDA 03
        Floor:       03
        Room:        3.3.109
krock1516
  • 343
  • 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 http://mywiki.wooledge.org/BashFAQ/001 – Sergiy Kolodyazhnyy Jan 19 '17 at 04:03

1 Answers1

3

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                                                                                           
#!/bin/bash
# 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