Add leading zero's to the number in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add leading zero's to the number in unix
# 8  
Old 11-25-2009
if you want exactly 5 0's no matter the length of the string then
x=`echo -e "450\n2345"`
for i in $x
do
z=`expr 5 + length $i`
printf "%0${z}d" $i
done
# 9  
Old 11-25-2009
Well, if one always wants exactly 5 0's, this could also be used:
Code:
echo "00000$x, 00000$y"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ho to remove leading zeros from a csv file which is sent from a UNIX script

Hi All, I am using a informatica job to create a csv file and a unix script the mail the generated file.Everything is working fine but I am not seeing leading zeros in the csv file sent in the mail.These zeros were present when the .csv file was generated by informatica procees. Is there any... (11 Replies)
Discussion started by: karthik adiga
11 Replies

2. Shell Programming and Scripting

How to add line number using UNIX command?

Hi, I am working on Shell script and I have a .dat file in which I want to add line numbers. Please see below example file Input File: 19523479811841494432C2013052700000000 19523479811730333980A2013052700000000 19523479811417044397I2013052700000000 19523479811205895810A2013052700000000... (7 Replies)
Discussion started by: jnrohit2k
7 Replies

3. UNIX for Dummies Questions & Answers

Add leading zeros to columns in a file

Hello Gurus, Quick question. I have a file with the following records: A~000000000000518000~SLP ~99991231~20090701~88.50~USD~CS~ A~000000000000518000~SLP ~99991231~20090701~102.00~USD~CS~ A~000000000000772000~SLP ~99991231~20100701~118.08~USD~CS~ I wold like to do the following: 1. Add... (1 Reply)
Discussion started by: chumsky
1 Replies

4. Shell Programming and Scripting

awk to remove leading zeros for a hex number

Is it possible by using awk to remove leading zeros for a hex number? ex: 0000000011179E0A -> 11179E0A Thank you! (4 Replies)
Discussion started by: carloszhang
4 Replies

5. Shell Programming and Scripting

Add leading zeros in floating point variable

I need to add leading zeros in a floating point numbers. The length of the number should be 13 including decimal. The input number is changing so number of leading zeros is not fix. For example input output 216.000 000000216.000 1345.000 000001345.000 22345.500 ... (4 Replies)
Discussion started by: reeta_shri
4 Replies

6. Shell Programming and Scripting

excel drops leading zeros while attaching through unix

HEllo All, when the data file is being attached and mailed as file1.csv, the column data( e.g: 88E00, 99E00, 77F12, 66H18). The data 88E00 and 99E00 is being converted to 88E+01, 99E+01. All other data is fine. I need the file attached as .csv from unix only. Is there a way we can manage... (2 Replies)
Discussion started by: OSD
2 Replies

7. UNIX for Dummies Questions & Answers

Triml leading zeros in unix

Hi All, How does one trim leading zero's in unix Thanks KP. (7 Replies)
Discussion started by: kingofprussia
7 Replies

8. 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

9. Shell Programming and Scripting

Add leading zeroes to numbers in a file

Hello, I am (trying) to write a script that will check to see how many users are logged on to my machine, and if that number is more than 60 I need to kill off all the oldest sessions that are over 60. So far I have been able to check how many users are on and now I am at the part where I have to... (3 Replies)
Discussion started by: raidzero
3 Replies

10. UNIX for Advanced & Expert Users

FTP Between UNIX & Windows drops leading blanks

I am using ftp on Solaris to transfer files to Windows 2000 that have leading blanks in the name (ex. " 12345A4444.BIN"). When they get to Windows the leading blanks are stripped off. Has anyone encountered this and is there a fix? If I use Hummingbird it is ok but that is not an option for... (6 Replies)
Discussion started by: worf52
6 Replies
Login or Register to Ask a Question