home *** CD-ROM | disk | FTP | other *** search
- 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
- From: bnb@ukc.ac.uk (B.N.Blackmore)
- Newsgroups: comp.unix.shell
- Subject: Re: padding things out
- Message-ID: <6709@falcon.ukc.ac.uk>
- Date: 27 Jan 93 18:00:27 GMT
- References: <1993Jan26.194057.1884@unix.brighton.ac.uk>
- Reply-To: bnb@ukc.ac.uk (The Impossible Dreamer)
- Organization: Computing Lab, University of Kent at Canterbury, UK.
- Lines: 35
- Nntp-Posting-Host: falcon.ukc.ac.uk
-
- 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,
- :other times 2 for a shortname) how can I determine whether I need 1 or 2
- :in a shell script?
-
- Try this for size it should do what you require (I hope)
-
- #!/bin/sh
- # A shell script to nicely format a file which is TAB seperated
- # By Brian Blackmore
- if test $# != 1
- then
- echo Usage: $0 file
- fi
- # Pass one to find out the length of the largest line
- maxlength=0
- # Note this is a TAB below The @ avoid splitting spaces
- for a in "`sed 's/ .*//' < $*|tr ' ' '@'`"
- do
- thislength="`echo $a|wc -c|sed 's/ //g'`"
- if test $thislength -gt $maxlength
- then
- maxlength=$thislength
- fi
- done
- # Pass two print out the lines
- awk -F' ' "{printf (\"%-${maxlength}s %s\\n\", \$1, \$2)}" < $*
- # Note the File seperator is a TAB
- --
- Brian Blackmore, Darwin College,
- The University of Kent at Canterbury, United Kingdom.
-