Operating System - HP-UX
1833772 Members
2606 Online
110063 Solutions
New Discussion

How to pad out lines of text?

 
SOLVED
Go to solution
Robert Fisher_1
Frequent Advisor

How to pad out lines of text?

Hi,

I have an application that transfers check clearing information to the bank using a modem. Each line of output must consist of exactly 80 characters followed by a CR-LF. My input file varies in length although every line is less than 80 characters. Each input line ends in a single linefeed. I would like to read the input one line at a time and remove the linefeed. Next append spaces until I reach 80 characters. Finally, I need to output the carriage return - linefeed characters.

Input file:
LOGDXARC 5865002572IYS1 000000000 C00000000000000
1HDR -001 12299-051 876500187232311819N CARD
000001256700000100008765001572122899
000001256800001769978765001572122899
000001256900000042808765001572122899
000001257000000369188765001572122899
000001287900005547818765001572122899
000001288000006997878765001572122899
000001288100000166928765001572122899
000001288200000544488765001572122899
000001288300000845008765001572122899
000001288400000006078765001572122899
000001288500000300008765001572122899
000001288600000337188765001572122899
000001288700000160218765001572122899
000001288800002493478765001572122899
000001288900000595388765001572122899
000001289000001051688765001572122899
1EOF 00319 00040605190113047345

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


I'm thinking of using sed but I can't seem to get the syntax right.

Can anyone get me started?

Thanks,
Bob
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to pad out lines of text?

Well, I would tend to use awk or Perl for this rather than sed.

awk '{ printf("%-80.80s\r\n",$0) } < infile > outfile

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to pad out lines of text?

Ooops, I done gone and hit the [SUBMIT] too quick. Here's a Perl method:

perl -n -e 'chomp; printf("%-80.80s\r\n",$_)' < infile > outfile

No points for this one, please.

Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to pad out lines of text?

Ooops again,

The awk example was missing a quote (though I 'spect you could have figured that out):

awk '{ printf("%-80.80s\r\n",$0) }' < infile > outfile

No points again, Clay
If it ain't broke, I can fix that.
Robert Fisher_1
Frequent Advisor

Re: How to pad out lines of text?

Hi,

Thanks!! I used the perl method. Is there an easy way to check the output without having to count all the spaces?

Bob
A. Clay Stephenson
Acclaimed Contributor

Re: How to pad out lines of text?

How about od?

od -v -Ad -ta outfile | pg

Man od for details.

If it ain't broke, I can fix that.
Robert Fisher_1
Frequent Advisor

Re: How to pad out lines of text?

Hi,

Thanks!!! Od worked and the file looks perfect!!!

Bob