Help needed in padding leading zeros


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in padding leading zeros
# 1  
Old 03-13-2008
Help needed in padding leading zeros

Hi all,
I have file with numeric values. I need to pad each value with leading zeros such that total lenght of each value is 16.

Example:
cat tmp.txt
502455
50255
5026
5027
5028

Output
0000000000502455
0000000000050255
0000000000005026
0000000000005027
0000000000005028

Any help is appreciated
Thanks,
Jak
# 2  
Old 03-13-2008
Code:
# awk '{printf "%015s\n", $0}' file
000000000502455
000000000050255
000000000005026
000000000005027
000000000005028

# 3  
Old 03-13-2008
Code:
printf "%.16d\n" $(<tmp.txt)

# 4  
Old 03-13-2008
Thanks guys!
Jak
# 5  
Old 07-02-2008
How do i pad the hexadecimal values with leading zeroes

hex=`echo "obase=16;$data" | bc`
paddedData=`printf "%s" $hex `
echo $paddedData

results in
0
1
9
A

i want
00
01
09
0A

Any help is appreciated
JAK
# 6  
Old 07-02-2008
paddedData=`printf "%02x" 0x$hex `
# 7  
Old 07-02-2008
Try like this:

Code:
"/" > x=A
"/" > echo $x | awk '{printf "%02s\n",$0}'
0A
"/" > x=9
"/" > echo $x | awk '{printf "%02s\n",$0}'
09

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

Help with adding leading zeros to a filename

Hi i need help in adding leading zero to filenames e.g file name in my folder are 1_234sd.txt 23_234sd.txt the output i need is 001_234sd.txt 023_234sd.txt can i do this shell scripting please help (2 Replies)
Discussion started by: rsmpk
2 Replies

4. Shell Programming and Scripting

Padding with zeros.

Hi Friends, I would like to left pad with "0's" on first column say (width six) I have a large file with the format: FILE: 1: ALFRED 84378 NY 8385: JAMES 88385 FL 323: SMITH 00850 TX My output needs to be like: 000001: ALFRED 84378 NY 008385: JAMES 88385 FL 000323: SMITH... (10 Replies)
Discussion started by: sbasetty
10 Replies

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

6. Shell Programming and Scripting

bash typeset padding with zeros

Hi everybody, I have a question about typesetting. I originally wrote a script for use with ksh and now I am on a system that I cannot modify, and it only has bash. In the original script I just did typeset -RZ4 variable and it would add the leading zeros. In bash, it doesn't work. I've... (2 Replies)
Discussion started by: jwheeler
2 Replies

7. Shell Programming and Scripting

Removing leading zeros from a variable

How do I remove or add leading zeroa from a variable. To make variable 10 characters long when adding zeros. (6 Replies)
Discussion started by: toshidas2000
6 Replies

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

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

10. HP-UX

Padding zeros after removing commas in file

Hi Gurus, There is a ASCII file in which a comma is used as a seperator for the amount field when the amount exceed seven digits: e.g. 0001300,000. Now, this comma needs to be removed from this field, after padding leading zeros (to maintain the ASCII positions) e.g. 00001300000.... (1 Reply)
Discussion started by: pranag21
1 Replies
Login or Register to Ask a Question