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
# 8  
Old 09-27-2006
I have closed the poll on this thread becasue it serves no useful purpose.
# 9  
Old 09-27-2006
To shereenmotor
Quote:
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
try this

Code:
zeros=`echo $variable | sed 's/\(0*\)[1-9][0-9]*/\1/'`

# 10  
Old 09-28-2006
Quote:
Originally Posted by anbu23
try this

Code:
zeros=`echo $variable | sed 's/\(0*\)[1-9][0-9]*/\1/'`

Anbu23, I already tried that, but output is same, here is the output:
Code:
$set -x
$read variable
+ read variable
001007
$zeros=`echo $variable | sed 's/\(0*\)[1-9][1-9]*/\1/'`
+ sed 's/\(0*\)[1-9][1-9]*/\1/'
+ echo 001007
+ zeros=00007

What you suggest now?

Thanks & Regards,
Tayyab
# 11  
Old 09-28-2006
How about this to set length of a variable to length of input variable and then do the arithmetic
Code:
read x
typeset -Z${#x} y
y=$(($x + 1))
print $y

cheers
# 12  
Old 09-28-2006
How about something like this:
Code:
read x
z=`echo $x | awk '{print length}'`
y=`expr $x + 1`
printf "%0"$z"d\n" $y

I couln't get the typeset command to work on my machine. It complained about the -Z being and invalid option. But I'm pretty sure awk is the same on most *nix systems For some reason the "y=$(($x+1))" subtracted 1 instead of adding it. Expr seemed to fix that. Very strange, that's never happend before.

Last edited by mph; 09-28-2006 at 11:49 AM..
# 13  
Old 09-28-2006
What happens if you just do $((x+1)) instead of $(($x+1)) ?
# 14  
Old 09-28-2006
To shereenmotor
Quote:
Originally Posted by anbu23
try this


Code:
zeros=`echo $variable | sed 's/\(0*\)[1-9][0-9]*/\1/'`

Anbu23, I already tried that, but output is same, here is the output:
Code:
$set -x
$read variable
+ read variable
001007
$zeros=`echo $variable | sed 's/\(0*\)[1-9][1-9]*/\1/'`
+ sed 's/\(0*\)[1-9][1-9]*/\1/'
+ echo 001007
+ zeros=00007What you suggest now?

Thanks & Regards,
Tayyab
Code:
zeros=`echo $variable | sed 's/\(0*\)[1-9][0-9]*/\1/'`

You used 1 in second range [1-9].Use 0 there
 
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