rchattam@isis.cs.du.edu (King Chattam) (06/04/91)
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? rchattam@nyx.cs.du.edu Thanks
cpcahil@virtech.uucp (Conor P. Cahill) (06/04/91)
rchattam@isis.cs.du.edu (King Chattam) writes: > Could someone please tell me how blanks can be appended to string vars >to make it fixed length? The following will do what you want: a=abc t="`expr \"$a \" : \"\(..........\)\"`" echo "x${t}x" You will get: "xabc x" which is $a plus 7 blanks. The x's are only used to mark the string so you can see that the blanks actually are there. -- Conor P. Cahill (703)430-9247 Virtual Technologies, Inc. uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160 Sterling, VA 22170
john@sco.COM (John R. MacMillan) (06/04/91)
| Could someone please tell me how blanks can be appended to string vars |to make it fixed length? If the fixed length is relatively short, something like the following in sh/ksh will do: var=foo # Pad to 10: 10 spaces 10 dots var=`expr "$var " : '\(..........\).*'`
tif@doorstop.austin.ibm.com (Paul Chamberlain) (06/05/91)
In article <1991Jun04.144951.28414@sco.COM> john@sco.COM (John R. MacMillan) writes: >| Could someone please tell me how blanks can be appended to string vars >If the fixed length is relatively short, ... else something like this will do it (width of 75, x's for demonstration): a=abc awk 'BEGIN {printf "x%-75sx\n", "'"$a"'"; }' /dev/null And gives you: xabc x Note that without the "-", it will do right justification. Paul Chamberlain | I do NOT speak for IBM. IBM VNET: PAULCC AT AUSTIN 512/838-9748 | ...!cs.utexas.edu!ibmchs!auschs!doorstop.austin.ibm.com!tif
lugnut@sequent.UUCP (Don Bolton) (06/06/91)
In article <1991Jun3.181447.12656@mnemosyne.cs.du.edu> rchattam@isis.cs.du.edu (King Chattam) writes: >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. > Awks printf("%10s%s\n",$1,"|") Or some such syntax should put out a "|" symbol at the 11th pos. (am not at my real desk to check syntax out) Or you could get more involved and use length to count the characters in your variable and a while i <= 10 print " "; ++i the tools are there anyhow. > Could someone please tell me how blanks can be appended to string vars >to make it fixed length? >rchattam@nyx.cs.du.edu >Thanks
lanzo@wgate.UUCP (Mark Lanzo) (06/06/91)
In a prior article rchattam@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.
jerry@ora.com (Jerry Peek) (06/08/91)
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
cudcv@warwick.ac.uk (Rob McMahon) (06/11/91)
In article <1991Jun3.181447.12656@mnemosyne.cs.du.edu> rchattam@isis.cs.du.edu (King Chattam) writes: >I have a shell var (length 0 to 10), which I want always to be output to a >file as fixed length 10. Strange no-one has mentioned Chris Torek's `printf': cudcv@shark [dcv] >% set var = gronk cudcv@shark [dcv] >% printf "x%10sx\n" $var x gronkx cudcv@shark [dcv] >% printf "x%-10sx\n" $var xgronk x cudcv@shark [dcv] >% /* * printf - duplicate the C library routine of the same name, but from * the shell command level. * * This version by Chris Torek, based on an earlier version by Fred Blonder. */ Cheers, Rob -- UUCP: ...!mcsun!ukc!warwick!cudcv PHONE: +44 203 523037 JANET: cudcv@uk.ac.warwick INET: cudcv@warwick.ac.uk Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England
Dan_Jacobson@ATT.COM (06/12/91)
>>>>> "Mark" == Mark Lanzo <lanzo@atesysv.UUCP> writes:
Mark> If you are using "ksh" [...]
Mark> myname=Mark
Mark> typeset -L10 myname
Mark> echo "XX${myname}XX"
Mark> should produce
Mark> XXMark XX
In my prompt I wanted to put $? (last command exit status) in a fixed
width... (PS1='$? '...) but I can't typeset -L3 ? or \?...
lanzo@wgate.UUCP (Mark Lanzo) (06/13/91)
"Mark" == Mark Lanzo <lanzo@atesysv.UUCP> [that's me] "Dan" == Dan_Jacobson@ihlpz.ATT.COM From prior posts: Mark> If you are using "ksh" [...] Mark> myname=Mark Mark> typeset -L10 myname Dan> In my prompt I wanted to put $? (last command exit status) in a fixed Dan> width... (PS1='$? '...) but I can't typeset -L3 ? or \?... Hmm. As far as I know, you're out of luck with this. You're right, "typeset" doesn't work on special parameters like "$?"; it just complains that "?" isn't a valid identifier. Even if "typeset" _did_ work on $?, I think you'd still be out of luck. Ksh evaluates PS1 and performs *parameter substitution* on the string, but it does not perform *command substitution* on it. (Drats!) In other words, you can't use the `command` or $(command) forms within PS1 in any meaningful fashion. For example, to display the current directory in the prompt: PS1='$PWD> ' # Works PS1='`pwd`> ' # Does not work PS1='$(pwd)> ' # Does not work I even tried screwier things, to see if any sort of recursive evaluation mechanisms could be faked out, such as PS1='${?:+`pwd`}' ... to no avail. If anyone else has a solution, I'd like to hear about it ... -- Mark --
vinoski@apollo.hp.com (Stephen Vinoski) (06/13/91)
In article <193@atesysv.UUCP> lanzo@atesysv.UUCP (Mark Lanzo) writes: >"Mark" == Mark Lanzo <lanzo@atesysv.UUCP> [that's me] >"Dan" == Dan_Jacobson@ihlpz.ATT.COM > >From prior posts: > Mark> If you are using "ksh" [...] > > Mark> myname=Mark > Mark> typeset -L10 myname > >Dan> In my prompt I wanted to put $? (last command exit status) in a fixed >Dan> width... (PS1='$? '...) but I can't typeset -L3 ? or \?... > >Hmm. As far as I know, you're out of luck with this. >You're right, "typeset" doesn't work on special parameters like "$?"; >it just complains that "?" isn't a valid identifier. If your ksh is newer than the 06/86 version, how about this: typeset -L3 st=0 trap 'st=$?' DEBUG PS1='$st ' -steve | Steve Vinoski (508)256-0176 x5904 | Internet: vinoski@apollo.hp.com | | HP Apollo Division, Chelmsford, MA 01824 | UUCP: ...!apollo!vinoski |
les@chinet.chi.il.us (Leslie Mikesell) (06/14/91)
>Mark> If you are using "ksh" [...]
I've done this in /bin/sh to get fixed width numbers:
case $VAR in
???) VAR=0$VAR
;;
??) VAR=00$VAR
;;
?) VAR=000$VAR
;;
esac
It seems faster than calling up sed or awk, at least for small widths.
Les Mikesell
les@chinet.chi.il.us