![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 | |||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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 |
|
|||||
|
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 Code:
echo $variable | awk '{printf "%0*d\n",length,$1+1}'
Code:
printf "%0${#variable}d\n" $(expr $variable + 1)
Last edited by Ygor; 09-27-2006 at 05:03 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|