Left pad data with spaces

I have data in a csv file that looks similar to this:
AEPCO, 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 241.20
LGGCO, 210.51  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 241.20

I need a script that will add double quotes around the second field and left pad it with spaces so the total length of the second field is 9 characters.  Like this:

AEPCO, "     0.00"  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 241.20
LGGCO, "   210.51"  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 0.00  , 241.20

I will be running this on an IBM AIX machine.

Any ideas would be appreciated.
Mark
9thTeeAsked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
Hi,

try this:

awk -F, '{gsub(" ","",$2); printf "%s, \"%9s\" , %s, %s, %s, %s, %s, %s, %s, %s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10}' inputfile.csv

wmp

0
 
smogadatConnect With a Mentor Commented:
The script has been tested on AIX with ksh and bsh
Assuming that only the second field needs to be modified and the rest should remain intact.

#!/bin/ksh
# the script has been
awk -F"," ' {

# trim and store second fild into str
str = $2
sub(/[ \t]+$/, "", str)

# print first two fields in required format
printf("%s,\"%9s\",", $1, str)

# print the rest of the line as is
for (i=3; i<NF; i++) printf("%s%s", $i, ",")

# last field and end of line
printf("%s\n", $NF)
} ' input.csv
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.