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!

Padding a file so all lines are 80 bytes long 1

Status
Not open for further replies.

dendenners

Programmer
Jul 17, 2001
110
IE
Hi,
I have a file most of whose records are 80 bytes long. However, in this big file there are a few rogue records that are not the correct length! How do I pad these lines with spaces so all the lines are the correct length? Thanks very much
 

Hello dendenners!

This is littlebit awkward awk solution of your problem:

Code:
awk 'length < 80 {
    print $0 substr(&quot;  - put here 80 spaces - &quot;,1,length - $0 + 1)
}

length = 80  { print }' inputfile

String with spaces must have 80 spaces.

I hope this helps.

Bye!

KP.
 
Krunek, you seem to have made a typo in your substr. I think it should be

print $0 substr(&quot; -- 80 spaces -- &quot;,1,80-length)

CaKiwi
 
Alternatively, use fold. Look up the man pages, I'm afraid I don't have access to my machines at the moment. HTH.
 

You're right, CaKiwi! Thank you for looking my answer and founding error.

God bless you! Bye!

KP.
 
Another way ...

awk '{printf &quot;%80s\n&quot;,$0}'

Jp.
 
.. but you need

awk 'printf(&quot;%-80s\n&quot;, $0)}' filename

to have the lines left-justified.

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top