Number with starting zero


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number with starting zero
# 1  
Old 05-05-2010
Number with starting zero

Hello,
How to check if any number (or string) starting with zero (0)? if it starts with zero, how to remove the starting zero?

TIA,
Prvn
# 2  
Old 05-05-2010
One way:

Code:
IN=$1
OUT=$( echo $IN | sed 's/^0*//' )
if [ ${#OUT} -lt ${#IN} ]
then
  echo "Leading zero(s) eliminated"
fi
echo "IN: $IN >> OUT: $OUT"
exit 0

Code:
[house@leonov] bash test.bash 123000
IN: 123000 >> OUT: 123000
[house@leonov] bash test.bash 000123000
Leading zero(s) eliminated
IN: 000123000 >> OUT: 123000

# 3  
Old 05-05-2010
Another way (bash or ksh) :
Code:
$ shopt -s extglob  # For bash only, not need for ksh
$ number=00001230
$ echo ${number##+(0)}
1230
$ number=120450
$ echo ${number##+(0)}
120450
$

Jean-Pierre.

Last edited by aigles; 05-05-2010 at 02:23 PM..
# 4  
Old 05-05-2010
Portable, pure sh ways Smilie


Always remove a single leading zero:
Code:
x=${x#0}

Remove a single leading zero unless it is the only digit (it leads into nothing):
Code:
[ "$x" != 0 ] && x=${x#0}


Always remove all leading zeroes:
Code:
while [ "$x" != "${x#0}" ]; do
    x=${x#0}
done


Remove all leading zeroes except if removing a zero would result in a null value:
Code:
while [ "$x" != "${x#0}" -a "$x" != 0 ]; do
    x=${x#0}
done


Regards,
Alister
# 5  
Old 05-05-2010
Code:
typeset -i x="0001234"
print $x

...or...

Code:
printf "%-10.0f\n" '0001234'

This User Gave Thanks to curleb For This Post:
# 6  
Old 05-05-2010
For removing all leading zeroes, that printf approach gets my vote. Nice.

You can simplify it a bit, and avoid possible, unnecessary spaces in the output:
Code:
x=$(printf '%.0f' "$x")

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Process only 4 digit odd number starting with zero

I am trying to process only IonCode_odd #'s (always 4 digits starting with zero), but the below isn't working as expected. Is there a better way? Thank you :). IonCode_0401_xxxx_xxxx_xxxx.bam IonCode_0401_xxxx_xxxx_xxxx.bam.bai IonCode_0401_xxxx_xxxx_xxxx.fastq... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

3. Shell Programming and Scripting

How to read a file starting at certain line number?

I am new to ksh scripts. I would like to be able to read a file line by line from a certain line number. I have a specific line number saved in a variable, say $lineNumber. How can I start reading the file from the line number saved in $lineNumber? Thanks! (4 Replies)
Discussion started by: dcowboys13
4 Replies

4. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

5. Shell Programming and Scripting

replace line starting with not a number

Dear users, I have a file like this: geometry,geometry_vertex_count,Id,strnum,platecode,datatype,dtnum,refnum,appearance,disappeara,color,geogdesc,datatype_ft_style,import_notes "<LineString><coordinates>-130.6539,51.5103,0 -130.7708,51.6287,0 -130.8356,51.6832,0 -130.9211,51.7772,0... (5 Replies)
Discussion started by: Gery
5 Replies

6. Solaris

awk - Print variable number of colums from a starting column

Hi guys, I usualy am able to google awk stuff but I can't find it so far and there are so many awking gurus here that I will give it a shot. I want to print $1;$3;"$5 up to the $NF". In other words, I can have 1000 colums, but need to have $5 up to the end. I started with the idea of... (2 Replies)
Discussion started by: plmachiavel
2 Replies

7. Shell Programming and Scripting

How to count the number of files starting with a pattern in a Directory

Hi! In our current directory there are around 35000 files. Out of these a few thousands(around 20000) start with, "testfiles9842323879838". I want to count the number of files that have filenames starting with the above pattern. Please help me with the command i could use. Thank... (7 Replies)
Discussion started by: atechcorp
7 Replies

8. Emergency UNIX and Linux Support

Urgent help pls.how to extract two lines having same starting number

Hi , I have a huge file like this =245 this is testing =035 abc123 =245 this is testing1 =035 abc124 =245 this is testing2 =035 abc125 =035 abc126 =245 this is testing3 here i have to pull out those lines having two =035 instead of alternative 035 and 245 i.e extract... (18 Replies)
Discussion started by: umapearl
18 Replies

9. Shell Programming and Scripting

awk if statement matching all lines starting w/ a number

Does awk have a syntax for a range of numbers or is this the best way? if ($1 >= 0 && $1 <= 9 ) (7 Replies)
Discussion started by: Arsenalman
7 Replies

10. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies
Login or Register to Ask a Question