Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filling in characters to line a file up

Status
Not open for further replies.

hcclnoodles

IS-IT--Management
Jun 3, 2004
123
GB
Hi there, Ive got a feeling this is quite complex but I have a Comma delimited file which is being transfered up to a mainframe and I have to get every column lining up , I need to add in characters to line it up (but different characters and in different positions based on what field/column it is.

For example here is my original file ......


12345,abcdef,123456789,abcd
123456,abcdef,1234567,abcd

and here is what i need doing ............

1) IF data in field 1 is less than 6 characters long then put enough spaces after the last character to make it up to 6

2) Fields 2 and 4 are fixed at 7 characters and will always be populated by 7 character data

3) IF data in Field 3 is less than 10 characters then place leading zero's in front until 10 characters long

So the end result will look like this...

12345 ,abcdef,0123456789,abcd
123456,abcdef,0001234567,abcd

all lined up lovely for processing

If anybody could give me a few tips on what i would need to do to get started on this and what programs i need to use etc then that would be fantastic

cheers
Gary

 
man awk (the printf function)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Somehing like this

BEGIN{FS=","}
{
printf ("%6s,%7s,%010d,%7s\n",$1,$2,$3,$4)
}

CaKiwi
 
Sorry the printf should be

printf ("%-6s,%7s,%010d,%7s\n",$1,$2,$3,$4)


CaKiwi
 
thanks Cakiwi, just one quick question though, how do I pass this function an input file ??? or for that matter specify an output file (if required)
 
sorry ignore that last question, i worked it out,

I have one further question if i may

I used your printf command but ammended it slightly (changed %7s to %-7s) but the %010d doesnt seem to put zero's before the string, just spaces ! see below

***the original file***

1234,abcdef,123456789,abcdefg
123456,abcdef,12345,abcdefg

***the result***

1234 ,abcdef, 123456789,abcdefg
123456 ,abcdef, 12345,abcdefg

How do I get `0` instead of a space for this field

cheers
 
very embarrassed, !! sorry, yes i got my syntax wrong

Last question, the field using %010s has financial data eg 1543.56 which i was hoping would be converted to 0001543.56 but for some reason it is appearing as 0000001543 , ignoring the decimal point and the 2 characters after it. Is this normal, and if so is there any way around it

cheers
Gary
 
Thanks for everyones help on this, in case anybody is interested, here is my final code

-----------------------------------

#!/bin/ksh
code='BEGIN{FS=","}
{
printf ("%-11s,%19s,%4s,%3s,%4s,%-10s,%07.2f,%14s,%-20s\n",$1,$2,$4,$6,$7,$8,$9,$10,$11)
}'

tr '\t' ',' < ./input_file | cut -d, -f-11 | tail +2 | awk "$code" > output_file
 
I think you could do it all in awk, something like this (untested)
#!/bin/ksh
code='NR>2{
printf ("%-11s,%19s,%4s,%3s,%4s,%-10s,%07.2f,%14s,%-20s\n",$1,$2,$4,$6,$7,$8,$9,$10,$11)
}'

awk "$code" input_file > output_file


CaKiwi
 
One all in one way:
awk -F'\t' ' '
NR>1{printf "%-11s,%19s,%4s,%3s,%4s,%-10s,%07.2f,%14s,%-20s\n",$1,$2,$4,$6,$7,$8,$9,$10,$11}
' input_file > output_f

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I presume by this you mean that i do not need to use the "cut -d, -f-11" command because the AWK script is extracting the fileds i want and disregarding the rest ?
 
With my script, no need of tr nor cut nor tail, ie all in one.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top