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
Replace one digit by two digit using sed Jae Shell Programming and Scripting 5 04-17-2008 09:32 PM
why "expr "${REPLY}" : '\([1-9][[:digit:]]*\)" can figure out whether it is a digit? sleepy_11 Shell Programming and Scripting 6 08-08-2007 12:51 AM
adding a 6 digit number retaining 0s on the left kanchan_cp Shell Programming and Scripting 8 03-08-2007 06:58 AM
Extracting 10 digit number from txt files c19h28O2 Shell Programming and Scripting 9 08-07-2006 03:06 PM
calculating a check digit jim9418 UNIX for Dummies Questions & Answers 1 02-16-2005 12:14 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-23-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 138
Arrow How to convert a 2 digit to 4 digit

Hi All,

How can i convert a number 24 to 0024
In the same way how can i convert 123 to 0123?
All this has to be done inside a script

Thanks in advance
JS
Reply With Quote
Forum Sponsor
  #2  
Old 04-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
If you have printf, that's the ticket.

Code:
vnix$ printf '%04i\n' 24
0024
If you don't have it in the shell, try awk.

Code:
echo 24 | awk '{ printf "%04i\n", $0 }'

Last edited by era; 04-23-2008 at 01:23 AM. Reason: awk as fallback
Reply With Quote
  #3  
Old 04-23-2008
Registered User
 

Join Date: Dec 2005
Posts: 74
if you would like the transformed value inside the same variable then you could use

one=1
one=`printf "%04i" $one`
Reply With Quote
  #4  
Old 04-23-2008
Registered User
 

Join Date: Jul 2006
Posts: 189
try typeset

$ typeset -R4Z tmp
$ tmp=23
$ echo $tmp
0023
$
Reply With Quote
  #5  
Old 04-23-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 138
Arrow A problem with awk

Hi All,

I have a file in teh format given below:

catanimal 0 animal 90 number90 cat_number_name
catanimal 1 animal 91 number91 cat_animal_name
catanimal 2 animal 92 number92 cat_name_animal


kiwiani 0 bird 90 number90 kiwi_number_name
kiwiani 1 bird 91 number91 kiwi_animal_name
kiwiani 2 bird 92 number92 kiwi_name_animal

cat 0 animal 90 number90 cat_number_name
cat 1 animal 91 number91 cat_animal_name
cat 2 animal 92 number92 cat_name_animal

bat 0 animal 90 number90 bat_number_name
bat 1 animal 91 number91 bat_animal_name
bat 2 animal 92 number92 bat_name_animal

When i use awk '$1 ~ /bat/ {if ($2==0){print $5} } ' file , i get the
result i want
But if i give awk '$1 ~ /cat/ {if ($2==0){print $5} } ' file, I get
result for $1=cat and $1=catanimal where as i want the result for only $1="cat"

Thanks in advance
JS
Reply With Quote
  #6  
Old 04-23-2008
Registered User
 

Join Date: Jul 2006
Posts: 189
awk '$1 ~ /^cat / {if ($2==0){print $5} } ' file,
Reply With Quote
  #7  
Old 04-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Or use equality (==) instead of pattern matching (~). The ~ operator will match the pattern anywhere in the field.

Code:
awk '$1 == "cat" && $2 == 0 { print $5 }' file
aju_kup's solution to use a better pattern is of course a perfectly good solution as well; just pointing out an alternative which may be more convenient in some situations.
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:22 PM.


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