home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / shell / 5516 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  1.7 KB

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!rpi!usc!howland.reston.ans.net!spool.mu.edu!agate!doc.ic.ac.uk!uknet!rook.ukc.ac.uk!falcon.ukc.ac.uk!bnb
  2. From: bnb@ukc.ac.uk (B.N.Blackmore)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: padding things out
  5. Message-ID: <6709@falcon.ukc.ac.uk>
  6. Date: 27 Jan 93 18:00:27 GMT
  7. References: <1993Jan26.194057.1884@unix.brighton.ac.uk>
  8. Reply-To: bnb@ukc.ac.uk (The Impossible Dreamer)
  9. Organization: Computing Lab, University of Kent at Canterbury, UK.
  10. Lines: 35
  11. Nntp-Posting-Host: falcon.ukc.ac.uk
  12.  
  13. In article <1993Jan26.194057.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. Try this for size it should do what you require (I hope)
  23.  
  24. #!/bin/sh
  25. # A shell script to nicely format a file which is TAB seperated
  26. # By Brian Blackmore
  27. if test $# != 1 
  28. then
  29.     echo Usage: $0 file
  30. fi
  31. # Pass one to find out the length of the largest line
  32. maxlength=0
  33. # Note this is a TAB below        The @ avoid splitting spaces
  34. for a in "`sed 's/    .*//' < $*|tr ' ' '@'`"
  35. do
  36.     thislength="`echo $a|wc -c|sed 's/ //g'`"
  37.     if test $thislength -gt $maxlength
  38.     then
  39.         maxlength=$thislength
  40.     fi
  41. done
  42. # Pass two print out the lines
  43. awk -F'    ' "{printf (\"%-${maxlength}s %s\\n\", \$1, \$2)}" < $*
  44. # Note the File seperator is a TAB
  45. --
  46. Brian Blackmore, Darwin College,
  47. The University of Kent at Canterbury, United Kingdom.
  48.