home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / shell / 5511 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.8 KB  |  42 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!enterpoop.mit.edu!ira.uka.de!slsvaat!us-es.sel.de!pan
  3. From: pan@us-es.sel.de (Pawel Nabe)
  4. Subject: Re: padding things out
  5. Message-ID: <1993Jan28.083829.20353@us-es.sel.de>
  6. Sender: news@us-es.sel.de
  7. Organization: SEL-Alcatel LTS Dept. US/ES
  8. References: <1993Jan26.194057.1884@unix.brighton.ac.uk> <1993Jan27.175539.17114@bnr.ca>
  9. Date: Thu, 28 Jan 93 08:38:29 GMT
  10. Lines: 30
  11.  
  12. In article <1993Jan27.175539.17114@bnr.ca>, webster@bnr.ca (Brent Webster) writes:
  13. |> In article 1884@unix.brighton.ac.uk, ndl@unix.brighton.ac.uk (Nathan Lock) writes:
  14. |> >How can I take a variable length field 
  15. |> >(e.g the gecos field in the password file)
  16. |> >and pad it out in a shell script so that it is always the same length 
  17. |> >for the next field to line up. 
  18. |> >(with a tab I will not always get the same column, sometimes I need 1,
  19. |> >other times 2 for a shortname) how can I determine whether I need 1 or 2 
  20. |> >in a shell script?
  21. |> >
  22. .....
  23. |> Try awk  ie
  24. |> % echo "biff stuff    junk"|awk -F"    " '{printf "%-30s %s\n",$1, $2}'
  25.  
  26. No, use buildin things of sh(1). This runs much faster than the awk(1) example.
  27.    while :
  28.          do
  29.               case "${field}" in
  30.                   ??????????*)
  31.                         break;;
  32.               esac
  33.               field="${field} "
  34.          done
  35. Now ${field} is at least 10 charcters long (the '*' at the end of ?????????? avoids infinite loops).
  36. The above loop should pad field with the right number of spaces to make it 10 characters long. You can get unexpected results if field already containted more than 10 chars or some funny chars like tabs or newlines,...
  37. -- 
  38.  
  39. Pawel Nabe                                   PaN Super Sonic Software Service
  40. Business: +49/711/821 71 786                 expert for hi tech & low budget
  41. Private:  +49/30/6 22 52 46 or +49/30/6 22 51 93
  42.