The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-06-2009
harry0013 harry0013 is offline
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
  #2 (permalink)  
Old 01-06-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
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
  #3 (permalink)  
Old 01-06-2009
harry0013 harry0013 is offline
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.
  #4 (permalink)  
Old 01-06-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
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
  #5 (permalink)  
Old 01-06-2009
harry0013 harry0013 is offline
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
  #6 (permalink)  
Old 01-06-2009
harry0013 harry0013 is offline
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.
  #7 (permalink)  
Old 01-06-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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