![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Triml leading zeros in unix | kingofprussia | UNIX for Dummies Questions & Answers | 7 | 08-07-2008 02:04 AM |
| Help needed in padding leading zeros | jakSun8 | Shell Programming and Scripting | 12 | 07-02-2008 04:41 PM |
| Removing leading zeros from a variable | toshidas2000 | Shell Programming and Scripting | 6 | 02-27-2008 10:13 AM |
| how to retain leading zeros | Manish Jha | Shell Programming and Scripting | 3 | 11-09-2006 02:59 PM |
| Leading zeros | wtofu | Shell Programming and Scripting | 1 | 09-16-2006 11:52 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Conversion to display leading zeros for numerics
I have the following script (which works fine), escept I don't know how to make the MONTH and DAY show up with leading zeros. I have a statement (not in this script) which will show this in a YYYYMMDD format, but the script makes the MONTH and DAY fields show single digits. For today, as an example, it will show 2005122 when I want it to show 20051202. Please tell me how to make it show leading zeros. Here's the code:
date '+%m %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - 1` case "$DAY" in 0) MONTH=`expr "$MONTH" - 1` case "$MONTH" in 0) MONTH=12 YEAR=`expr "$YEAR" - 1` ;; esac DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1` esac echo "Yesterday was: $MONTH $DAY $YEAR" } Thanks. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Ok i dont know much about scripting but what if in the $MONTH and $DAY fileds you make it 0 $MONTH and 0$DAY. i dont know if scripting is like html where it just ignores zeros until specifically told to recognize them or what just a thought.
|
|
#3
|
|||
|
|||
|
man printf
oh, and your script won't quite work. (did I pass? Carl |
|
#4
|
||||
|
||||
|
You can use the typeset command in ksh to add leading zeros. Specifically typeset -Zn, where n is the total length that you want the numeric string to be.
Code:
$ dsimpg1=20 $ echo $dsimpg1 20 $ typeset -Z3 dsimpg1 $ echo $dsimpg1 020 |
|
#5
|
|||
|
|||
|
Quote:
Thanks. Carl |
|||
| Google The UNIX and Linux Forums |