home *** CD-ROM | disk | FTP | other *** search
- 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
- From: kenneths@ai.mit.edu (Kenneth J. Schneider)
- Newsgroups: comp.unix.shell
- Subject: Re: padding things out
- Date: 27 Jan 1993 17:36:48 GMT
- Organization: MIT Artificial Intelligence Laboratory
- Lines: 26
- Distribution: world
- Message-ID: <1k6h7gINN7iv@life.ai.mit.edu>
- References: <1993Jan26.194057.1884@unix.brighton.ac.uk>
- NNTP-Posting-Host: granola.ai.mit.edu
-
- In article <1993Jan26.194057.1884@unix.brighton.ac.uk>, ndl@unix.brighton.ac.uk
- (Nathan Lock) writes:
- > How can I take a variable length field
- > (e.g the gecos field in the password file)
- > and pad it out in a shell script so that it is always the same length
- > for the next field to line up.
- > (with a tab I will not always get the same column, sometimes I need 1
-
- the command rs works rather well, but you may not have it available.
-
- if not, here is a awk script:
-
- set max = `awk 'BEGIN { max=0}{if (length($1) > max) max = length($1);}END{print
- max}' < inputfile` # finds width of largest field in first colum
-
- @ max = $max + 2 # put in no. of extra spaces you want after the first col.
-
- awk '{printf("%-'$max's.", $1); for(i=2;i<NF;i++) printf ("%s ", $i); print""}'
- < inputfile
-
-
- to do this with different columns change the $1 in the first and third line to the
- desired col number.
-
- --ken
-
-