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!

Record padding with spaces 2

Status
Not open for further replies.

Calator

Programmer
Feb 12, 2001
262
AU
I need to process a file, and padd each record with a different number of spaces, depending on record type stored in the first 4 characters of each record, eg:

record type 1: padd rec with spaces to a total length of 36
record type 2: padd rec with spaces to a total length of 248
etc

Anyone has some sample code or a simple solution?
 
Try the following awk script

substr($0,1,4)+0==1 {printf("%-36s\n",$0)}
substr($0,1,4)+0==2 {printf("%-248s\n",$0)}
...


CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Thanks CaKiwi,

my record types are like "ECC1" etc, would the following be still correct:

substr($0,1,4)+0=="ECC1" {printf("%-36s\n",$0)}

and what exactly the +0 stands for?

Note that I need the record to end in CR + LF, will the supplied format achive that?

 
and also:

the code only returns those records that match by record type; we also need that any other record types, not listed, to be returned as unchanged. How can this be achieved?
 
Try something like this:
Code:
/^ECC1/{printf("%-36s\n",$0);next}
/^ECC2/{printf("%-248s\n",$0);next}
{print}


Hope This Help
PH.
 
To end in CR + LF use: printf("%-36s[red]\r[/red]\n",$0)
 
Good catch Ygor.
Code:
/^ECC1/{printf("%-36s\r\n",$0);next}
/^ECC2/{printf("%-248s\r\n",$0);next}
{printf("%s\r\n",$0}

Hope This Help
PH.
 
The +0 causes the value to be treated as a number rather than as a string so that, for example, "0001" will compare equal to " 1". Since your record types are strings you can just use a reguar expression as suggested by PHV.

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top