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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Retrieve the first 9 digits from the numeric value sollins Shell Programming and Scripting 10 07-01-2008 04:19 AM
Digits display Spoorthi16 UNIX for Dummies Questions & Answers 3 10-01-2007 08:26 PM
Only Digits as input namishtiwari UNIX for Dummies Questions & Answers 2 08-21-2007 06:23 AM
How to cut last 10 digits off psarava Shell Programming and Scripting 4 08-29-2006 03:52 AM
restrain the number of digits of a PID mlefebvr UNIX for Advanced & Expert Users 1 05-27-2002 09:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-06-2009
Registered User
 

Join Date: Jan 2009
Posts: 12
Formatting digits

I want to check the argument in KSH. If the user type in the prompt 'find 3' it will format 3 to 003 to match the data in the text file. Same as with 10 to 010. Always begins with 0.

eg.

>find 3

Output:
003

>find 30
Output:
030
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-06-2009
Moderator
 

Join Date: Feb 2007
Posts: 3,547
There is a utility with the name find, don't use the name find for your script.
You can use printf to format the data, something like this to print the 1st argument:

Code:
data=`printf "%03d" $1`
Regards
Reply With Quote
  #3 (permalink)  
Old 01-06-2009
Registered User
 

Join Date: Jan 2009
Posts: 12
Sorry..'find' is just a sample, the actual name is 'findlog'.
Is printf works in ksh? I'm just a newbie. Thank you.
Reply With Quote
  #4 (permalink)  
Old 01-06-2009
Moderator
 

Join Date: Feb 2007
Posts: 3,547
Quote:
Originally Posted by harry0013 View Post
Sorry..'find' is just a sample, the actual name is 'findlog'.
Is printf works in ksh? I'm just a newbie. Thank you.
Yes, it should work in ksh.

Regards
Reply With Quote
  #5 (permalink)  
Old 01-06-2009
Registered User
 

Join Date: Jan 2009
Posts: 12
I tried to test the below code but it is showing me odd results

data=`printf "%03d" $1`
echo $data

Sample test:
> findlog.sh 001
001
> findlog.sh 020
016
> findlog.sh 035
029
> findlog.sh 099
printf: 099: not completely converted
000
Reply With Quote
  #6 (permalink)  
Old 01-06-2009
Registered User
 

Join Date: Jan 2009
Posts: 12
The 1 or 2 digits is ok but when the 3 digit is used which is the correct argument, it is showing wrong number. eg 035 should also result in 035. Thank you.
Reply With Quote
  #7 (permalink)  
Old 01-06-2009
Moderator
 

Join Date: Feb 2007
Posts: 3,547
It's a strange behaviour of the built-in printf statement in ksh, try it with awk:

Code:
data=`echo $1 | awk '{printf "%03d", $0}'`
echo $data
Regards
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
None

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 12:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66