Padding with zeros.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Padding with zeros.
# 1  
Old 02-06-2007
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 00850 TX


Thanks in advance for your help
# 2  
Old 02-07-2007
Code:
#! /bin/ksh

typeset -RZ 6 code

while IFS=':' read first rest
do
    code=${first}
    echo "${code}: ${rest}"
done < input.txt

# 3  
Old 02-07-2007
Code:
while IFS=':' read first rest
do
    printf "%06d:%s\n" $first "$rest"
done< file

# 4  
Old 02-07-2007
Another way:
Code:
awk -F: '{ printf "%06d: %s\n", $1,$2 }' input.txt

# 5  
Old 05-13-2009
Padding with zeros

Hi all,

Referencing the awk statement here
Code:
awk -F: '{ printf "%06d: %s\n", $1,$2 }' input.txt

, I want to apply this concept to the 5th field, where I am padding 6 leading zeroes, and the rest of my fields stay the same. I tried the following:

Code:
awk -F, '{ printf "%06d, %s\n", $6,$7 }' test1.csv > test2.csv

but it won't output my first five fields or fields after the 6th field.

Please advise. Thanks much!
Lim
# 6  
Old 05-13-2009
Code:
 echo "1 2 3 4 5 6 7 8 9" | awk '{ $6=sprintf("%06s", $6); print $0}'
1 2 3 4 5 000006 7 8 9

# 7  
Old 05-13-2009
Thanks Jim. This looks like it would do the trick. I can't get it working with my code though. I started using this:
Code:
while IFS=',' read first rest
do
  printf "%07d,%s\n" $first "$rest" >> newfile.csv
done< file.csv

which will pad the first field. But I would like to pad the second field. How do I get the code above to pad the second? Thanks much!

Here's the input file:
"100","100","ABC"
"100","200","ABC"
"100","300","ABC"

Here's the desired output:
"100","0000100","ABC"
"100","0000200","ABC"
"100","0000300","ABC"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Padding a csv value with 0's

I have this csv file that I would like to sort on the 20th and 21st field. They are high lighted below. My challenge is that when I sort on those fields they are not in order as I would have liked. It seems like I have to pad those fields to the longest value in that fields data. ... (6 Replies)
Discussion started by: GroveTuckey
6 Replies

2. Shell Programming and Scripting

bash padding

Hi all Is there a way to pad the output of a bash script see that code below for i in `sed -n '/Start Printer/,/End Printer/p' /u/ab/scripts/hosts.conf | awk '!/^#/ {print $2}' | egrep -v 'broke|primera' `; do pages=`snmpget -Ov -v1 -c public $i sysLocation.0 | awk '{print $2}'` ... (3 Replies)
Discussion started by: ab52
3 Replies

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

4. UNIX for Dummies Questions & Answers

Zero padding dates

I have a file with records containing dates like: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|2007-6-6|MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|2003-11-6|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| How can I get the date fields in each of my records to be... (1 Reply)
Discussion started by: ChicagoBlues
1 Replies

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

6. Shell Programming and Scripting

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... (12 Replies)
Discussion started by: jakSun8
12 Replies

7. Shell Programming and Scripting

Padding in Unix

I have a file with different character counts on each line how do i make it with unique character counts. example: 1st line : ABCD 011 XYZ 0000 YYYY BBB TEADINGDA 2nd line: ABCD 011 xys 0010 YYYY BBB TEAD 3rd line : ABCD 022 YXU 000 UUU BBB TE 1st line is 43... (3 Replies)
Discussion started by: rudoraj
3 Replies

8. Programming

Byte Padding

Hi, Can someone explain what is byte padding? For ex: struct emp{ char s; int b; char s1; int b1; long b3; char s3; } What will be the size of this structure? Thanks (6 Replies)
Discussion started by: naan
6 Replies

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

10. UNIX for Dummies Questions & Answers

Padding

Hi Can anyone tell me how to pad zeroes on the left side to a numeric string in unix shell scripting Your answer is very much appreciated Thanks Vijay (2 Replies)
Discussion started by: vijaygopalsk
2 Replies
Login or Register to Ask a Question