adjusting string var to fixed length

Mark Lanzo lanzo at wgate.UUCP
Thu Jun 6 06:29:20 AEST 1991


In a prior article rchattam at isis.cs.du.edu (King Chattam) wrote:
    Netters,
      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?

You need to include more information in your posts.  Remember that not
everyone is working on the same type of system that you are.

What shell are you using?  If you are using "ksh", then it is very easy to
do what you want:

	typeset -L10 shell_variable

for example:

	myname=Mark
	typeset -L10 myname
	echo "XX${myname}XX"	

should produce
        XXMark      XX

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.  "Expr" or "sed" or "awk"
could be used, in a clumsy fashion.  For example, using expr with csh:

    set name=Mark
    set padded_name = "`expr substr $name:q'XXXXXXXXXX' 1 10`"
    # Use 10 spaces where I have shown the X's above.



More information about the Comp.unix.shell mailing list