The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2005
Registered User
 

Join Date: Sep 2001
Posts: 20
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.
Reply With Quote
Forum Sponsor
  #2  
Old 12-02-2005
Registered User
 

Join Date: Oct 2005
Posts: 50
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.
Reply With Quote
  #3  
Old 12-02-2005
Registered User
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 401
man printf

oh, and your script won't quite work.

(did I pass? )

Carl
Reply With Quote
  #4  
Old 12-02-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
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
Reply With Quote
  #5  
Old 12-02-2005
Registered User
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 401
Quote:
Originally Posted by blowtorch
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
Oh, I like that one. I'll have to make a note of it for a script I have at work.

Thanks.

Carl
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0