Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!ora!jerry From: jerry@ora.com (Jerry Peek) Newsgroups: comp.unix.shell Subject: Re: adjusting string var to fixed length Message-ID: <1991Jun08.084507.5750@ora.com> Date: 8 Jun 91 08:45:07 GMT References: <1991Jun3.181447.12656@mnemosyne.cs.du.edu> <190@atesysv.UUCP> Organization: O'Reilly and Associates Inc., Cambridge, MA Lines: 30 In article <190@atesysv.UUCP> lanzo@atesysv.UUCP (Mark Lanzo) writes: > In a prior article rchattam@isis.cs.du.edu (King Chattam) wrote: > I have a shell var (length 0 to 10), which I want always to be output > to a file as fixed length 10. I tried sed, awk etc to adjust the string > length to 10, but did not work. > > Could someone please tell me how blanks can be appended to string vars > to make it fixed length? ... > If you are using "sh" or "csh", then it's going to be harder as there isn't > anything built-in to the shell to do it for you. Mark probably didn't mention this built-in because it's sorta ugly. ;-) But I've used "case" for all kinds of things. It's built in to the shell, so it often runs faster than starting a process like expr or awk (expecially awk!) to do the same thing in a cleaner-looking way. Here goes... hold your nose... :-) case "$var" in "") var=" " ;; ?) var="$var " ;; ??) var="$var " ;; ...etc...etc... ?????????) var="$var " ;; ??????????) ;; # not needed unless you also use the *) below *) # add code here if you want to do handle too-long $var ;; esac --Jerry Peek, O'Reilly & Associates jerry@ora.com or uunet!ora!jerry