Leading Zero's


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Leading Zero's
# 1  
Old 12-19-2003
Leading Zero's

I'm trying to write a script that compare's some numeric values in a file, but in one of the files the value always has leading zero's. For example -

temp_8719_LM344_20031216091826: printera : 552 : 276 : 552 : 276

temp_8719_LM344_20031216091826: printera : 000552 :000276 : 000552 : 000276

How or what coding can I use to ignore these leading zeros?
# 2  
Old 12-19-2003
Good Morning,

well it depends on how many digits your numbers are max. If you can say, that you have only numbers with maxium 10 digits, you can read the values, and convert them to your maxiumum digit size with leading zeros, so you will have on both sides the same format and you can easily compare.

If you don't have a maxium size of digits, you should read the values as string first and use awk to remove leading zeros.

Regards
malcom
# 3  
Old 12-19-2003
Thanks for your reply

What is the best and most efficient coding to use in awk?
# 4  
Old 12-19-2003
In awk...

awk '{gsub(": *0*",": ")};1' $InFile > $OutFile

In sed...

sed 's/: *0*/: /g' $InFile > $OutFile

However, this conversion is not necessary if you are comparing numbers in a script when you use "-eq" and "-ne" instead of "=" and "!="
# 5  
Old 12-19-2003
Thanks very much
# 6  
Old 12-19-2003
However, this conversion is not necessary if you are comparing numbers in a script when you use "-eq" and "-ne" instead of "=" and "!="

Ygor brings up a good point that many people either forget or ignore. Integer comparison uses -eq, -lt, -gt, -ge etc while string comparison is done using = !=
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Leading blanks

Hi Ich have a list as follows 73 5 100 45 81 4 and I would like to have an output (on screen) like that 73 5 100 45 81 4 (6 Replies)
Discussion started by: lazybaer
6 Replies

2. Shell Programming and Scripting

Numbers with leading zeros

Hi, i have a variable which conatins values like 00001,0003,00067,00459. I want to use the values one by one and in the same form as they are like 00001,0003,00067,00459. Also can anyone tell me how to increment those numbers by 1,keeping the format as same like 00002,0004,00068,00460.... (5 Replies)
Discussion started by: arijitsaha
5 Replies

3. Shell Programming and Scripting

awk and leading zeroes

I have the following script that renames filenames like: blah_bleh_91_2011-09-26_00.05.43AM.xls and transforms it in: 91_20110926_000543_3_blih.xls for a in *.xls; do b="$(echo "${a}" | cut -d '_' -f4)" dia=`echo ${b} | cut -c9-10` mes=`echo ${b} | cut -c6-7` anio=`echo ${b} | cut -c1-4`... (4 Replies)
Discussion started by: Tr0cken
4 Replies

4. Shell Programming and Scripting

help in retaining leading zero

Hello. I'm trying to add multiple numbers with varying length and with leading zeroes in it. However, I'm getting the sum (totalHashAccountNumber) without the leading zeroes in it. How do I retain the leading zeroes? Please pardon the lengthy code.. I'm getting the hash account number from 2... (2 Replies)
Discussion started by: udelalv
2 Replies

5. Shell Programming and Scripting

Padding leading zero

hi All i am new to linux... source txt .. 281-BUM-5M BUM-5M 0 0 282-BUM-5M BUM-5M 0 0 83-BUM-5M BUM-5M 0 0 is it possible to use bash script to convert to (remove the "-" and fill up to 4 digit" ? 0281 BUM-5M BUM-5M 0 0 0282 BUM-5M BUM-5M 0 0 0083 BUM-5M BUM-5M 0 0 thanks a ... (5 Replies)
Discussion started by: samoptimus
5 Replies

6. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

7. UNIX for Dummies Questions & Answers

Add leading zero

Hi there, I'm trying to add to a number leading zeros if it's just one digit. My code looks like this: d=`date +%d` if ; then d=$((d-1)) # yesterday if (length($d) -eq 1); then d=0$d # this doesn't work if it's the 1st of the month :-( fi ... $d... (4 Replies)
Discussion started by: borobudur
4 Replies

8. UNIX for Dummies Questions & Answers

removing leading zero

Hi, I do have a code as follows: function deHex { if ]; then deHexDate=${1:1:$2} fi } The description given for the above code is as follows: # description: removes leading zero so value will not be recognized as hex # usage: deHex <value_parameter> <value_input_length> ... (0 Replies)
Discussion started by: risshanth
0 Replies

9. Shell Programming and Scripting

how to retain leading zeros

Hi All, I am working with a fixed width file Forrmat. C1 Number (10,3) C2 Number (10,3) e.g. c1= 0000000100.000 c2= 0000000020.000 0000000100.0000000000020.000 I have to perform c1 - c2 . i.e. I want answer to be 0000000080.000. but I am loosing the leading zeros( only getting... (3 Replies)
Discussion started by: Manish Jha
3 Replies

10. Shell Programming and Scripting

Leading zeros

How to insert leading zeros into a left-justisfied zip code? e.g. Zip code is written as 60320 which is left-justified to make it be read as 0060320. We have to move it to right-justifiable then insert 2 leading zeros into it... ;) (1 Reply)
Discussion started by: wtofu
1 Replies
Login or Register to Ask a Question