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

  1. Path: sparky!uunet!digex.com!intercon!udel!bogus.sura.net!howland.reston.ans.net!spool.mu.edu!yale.edu!yale!mintaka.lcs.mit.edu!ai-lab!ai.mit.edu!kenneths
  2. From: kenneths@ai.mit.edu (Kenneth J. Schneider)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: padding things out
  5. Date: 27 Jan 1993 17:36:48 GMT
  6. Organization: MIT Artificial Intelligence Laboratory
  7. Lines: 26
  8. Distribution: world
  9. Message-ID: <1k6h7gINN7iv@life.ai.mit.edu>
  10. References: <1993Jan26.194057.1884@unix.brighton.ac.uk>
  11. NNTP-Posting-Host: granola.ai.mit.edu
  12.  
  13. In article <1993Jan26.194057.1884@unix.brighton.ac.uk>, ndl@unix.brighton.ac.uk
  14. (Nathan Lock) writes:
  15. > How can I take a variable length field 
  16. > (e.g the gecos field in the password file)
  17. > and pad it out in a shell script so that it is always the same length 
  18. > for the next field to line up. 
  19. > (with a tab I will not always get the same column, sometimes I need 1 
  20.  
  21. the command rs works rather well, but you may not have it available. 
  22.  
  23. if not, here is a awk script:
  24.  
  25. set max = `awk 'BEGIN { max=0}{if (length($1) > max) max = length($1);}END{print 
  26. max}' < inputfile`      # finds width of largest field in first colum
  27.  
  28. @ max = $max + 2        # put in no. of extra spaces you want after the first col.
  29.  
  30. awk '{printf("%-'$max's.", $1); for(i=2;i<NF;i++) printf ("%s ", $i); print""}' 
  31. < inputfile
  32.  
  33.  
  34. to do this with different columns change the $1 in the first and third line to the
  35. desired col number.
  36.  
  37. --ken
  38.  
  39.