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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
unzip .zip file and list the files included in the .zip archive oracledev UNIX for Dummies Questions & Answers 5 04-08-2008 04:49 PM
Addition problem onlyroshni Shell Programming and Scripting 2 12-10-2007 02:11 PM
prefixing filenames ravi raj kumar Shell Programming and Scripting 2 11-20-2007 06:17 AM
Unix addition ( Row wise) gauravgoel Shell Programming and Scripting 3 05-17-2007 04:27 AM
PS1 with date stamp included in prompt kymberm Shell Programming and Scripting 7 03-28-2003 05:13 PM

View Poll Results: Do you find this post helpful
Very Helpful 0 0%
Not helpful 0 0%
Voters: 0. You may not vote on this poll

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-26-2006
sivasenthil_k sivasenthil_k is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 14
Question Addition with the prefixing zeros included

Hi

I need to do an add function but it should include the prefixed zeros

For Example
i=00123
j=`expr $i + 1`

The output it shows is 124 but I want the output to be 00124

Could any one help me finding out how to do this

Thanks
  #2 (permalink)  
Old 09-26-2006
pressy's Avatar
pressy pressy is offline Forum Staff  
solaris cultist
  
 

Join Date: Aug 2003
Location: Vienna / Austria (Europe) [EARTH]
Posts: 726
hi,

i moved your post from "news, links, events announcements" to "UNIX for Dummies Questions & Answers" and closed the poll.

and, here is your answer:
Code:
root@mp-wst01 # i=00123
root@mp-wst01 # j=`printf %05d $(expr $i + 1)`
root@mp-wst01 # echo $j
00124
regards pressy

Last edited by pressy; 09-26-2006 at 06:20 AM..
  #3 (permalink)  
Old 09-26-2006
sivasenthil_k sivasenthil_k is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 14
Smile

Hi

Thanks

I posted in the wrong group by mistake , Sorry about that

Regarding my query

I don't know what would be the length of the input string , actually 00123 would be the input from the user .The script would look like

read i
j=`printf %05d $(expr $i + 1)`
echo $j

The user can input 00123 or 0123 or 00000123 for which I want the output to be 00124 , 0124 , 00000124 respectively

Siva
  #4 (permalink)  
Old 09-27-2006
sivasenthil_k sivasenthil_k is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 14
Hi

I have put some simple logic in place to do the trick for me , the code now looks like

read variable
length=${#variable}
y=`expr $variable + 1`
length1=${#y}
if [ $length1 != $length ]
then
t=`expr $length - $length1`
i=1
ttt=$y
while [ $i -le $t ]
do
i=`expr $i + 1`
ttt=`echo "0$ttt"`
done
echo $ttt
else
echo $y
fi

Sample outputs are
$ ./ttt
9
10
$ ./ttt
99
100
$ ./ttt
1
2
$ ./ttt
100
101
$ ./ttt
01
02
$ ./ttt
00101
00102
$ ./ttt
009
010
$

However I still feel there could be some simple logic to do the same , any suggestions please ??

Thanks
SIva
  #5 (permalink)  
Old 09-27-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
What about this:
Code:
read variable
zeros=`echo $variable | sed 's/\(0*\)[1-9]*/\1/'`
y=`expr $variable + 1`
print $zeros$y
Regards,
Tayyab
  #6 (permalink)  
Old 09-27-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
That doesn't work. Here $ represents the ksh prompt...
Code:
$ set -x
$ read variable
+ read variable
001007
$ zeros=`echo $variable | sed 's/\(0*\)[1-9]*/\1/'`
+ echo 001007
+ sed s/\(0*\)[1-9]*/\1/
+ zeros=00007
$ y=`expr $variable + 1`
+ expr 001007 + 1
+ y=1008
$ print $zeros$y
+ print 000071008
000071008
Try this instead...
Code:
echo $variable | awk '{printf "%0*d\n",length,$1+1}'
Or...
Code:
printf "%0${#variable}d\n" $(expr $variable + 1)

Last edited by Ygor; 09-27-2006 at 05:03 AM..
  #7 (permalink)  
Old 09-27-2006
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
I have closed the poll on this thread becasue it serves no useful purpose.
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 10:18 AM.


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