Addition with the prefixing zeros included


View Poll Results: Do you find this post helpful
Not helpful 0 0%
Very Helpful 0 0%
Voters: 0. This poll is closed

 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Addition with the prefixing zeros included
# 1  
Old 09-26-2006
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  
Old 09-26-2006
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 07:20 AM..
# 3  
Old 09-26-2006
Bug

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  
Old 09-27-2006
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  
Old 09-27-2006
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  
Old 09-27-2006
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 06:03 AM..
# 7  
Old 09-27-2006
Ygor you are right, script fails if there is any zero in number, what you suggest to fix this. Idea was to grab zero's until the first occurrence of any non-zero digit but it didn't work Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date included

#!/usr/bin/ksh # This script reads the *.rpt files created by xenos during job execution. # The job name, jobid, number of pages processed and pdf files created for each job # are added to the xenoshistory.txt file Currently the ouput file looks like this but I need a date where the... (3 Replies)
Discussion started by: bcarosi
3 Replies

2. Shell Programming and Scripting

Rm -rf file.txt~ included in the first step?

I need to shred and delete a file after a certain time. Therefore I use shred -z /path/to/file.txt | rm -rf /path/to/file.txtIt works well, but typing in that very directory ls -shiI still see the so called backup-copy lets say file.txt~ When running bleachbit it will disappear thoroughly.... (3 Replies)
Discussion started by: 1in10
3 Replies

3. Shell Programming and Scripting

Prefixing test case methods with letter 'test'

Hi, I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the... (5 Replies)
Discussion started by: royalibrahim
5 Replies

4. Solaris

What update level included fcinfo?

Hi all first post here. quick question what Solaris 10 update level included this utility? I have a bunch of Sun boxes at varying levels of Solaris 10 some have it some do not. When I do my install I take the full option for the packages to install on all the systems. thanks in advance... Mike (4 Replies)
Discussion started by: mike_243us
4 Replies

5. Shell Programming and Scripting

matching a pattern in a file & prefixing a word

Hi, In my shell script i have to match a patten in a file , if found i have to prefix the entair line by a "word" eg. pattern = "aaa" prefix= #123 file: bbbb xxx zzzz aaaa qqqq kkkk outPut file: bbbb xxx ... (5 Replies)
Discussion started by: shivarajM
5 Replies

6. Shell Programming and Scripting

CSV formatting with prefixing, appending and padding field

Hi I have a very large csv file with some hundreds of thousands of rows of data. The data is in the following format: Up to four alpha numeric characters for the first word. This is either set as 2 characters followed by 2 spaces, or as a single 4character word. This is then followed by an 8... (7 Replies)
Discussion started by: meself
7 Replies

7. Shell Programming and Scripting

prefixing filenames

Hi,:cool: I have a list of files in a directory.I need to store them in a file with the prefix of @ by using a command.. ex:@p_po.plb @p_ebiz_roster_data.plb any idea pls. cheers RRK (2 Replies)
Discussion started by: ravi raj kumar
2 Replies
Login or Register to Ask a Question