Operating System - HP-UX
1827516 Members
2731 Online
109965 Solutions
New Discussion

Re: Formating output to fixed lenght

 
SOLVED
Go to solution
Victor Pavon
Advisor

Formating output to fixed lenght

Hello Great Ones:
I???m trying for the last few days to get a formatted output on a file.
I have 100???s of windows *82.txt files with this format:

"header*GSIFDS*OO*T"
"H","15650010","1","5/8/2002","5/13/2002","somemore123","B24","B0"
"I","050526","5","16.54","NONE"
"I","050535","6","26.8","NONE"
"I","000936","7","29.92","NONE"
"I","014160","8","47","NONE"
"I","KI21340","9","17.18","NONE"
"I","04460","10","38.7","NONE"
"C","NONE"
"C","NONE"
"C","NONE"
"trailer*GSIFDS*OO*B24*12"

These are comma separated fields on a database, the DBM reads these lines sequentially (quotes and all) and imports them into its own proprietary format.
My task in the UX world is to strip all \r and \n to make it a one liner file ??? which I was able to do with:
tr -d "[\015\012]" < test > testout

My problem is to format each line with a total length of 378 bytes disregarding the length of actual data characters.
My latest script looks like:

rm -f test
cat *82.txt | while read LIN
do
DAT=`printf "%.378s\n" $LIN`
echo $DAT >> test
done
tr -d "[\015\012]" < test > testout
exit

But this is giving me identical *82.txt and test files (no trailing white spaces)
I???ve seen hundreds of posting on getting rid of white spaces but nothing on injecting trailing chr(20).
I???ll appreciate any cmments. Thanks, Victor
8 REPLIES 8
Sachin Patel
Honored Contributor

Re: Formating output to fixed lenght

Hi Victor,
I don't know how to check for fixed length string.

But here is inserting blanck space
#insert 5 blank spaces at beginning of each line (make page offset)
sed 's/^/ /'

Sachin
Is photography a hobby or another way to spend $
A. Clay Stephenson
Acclaimed Contributor

Re: Formating output to fixed lenght

Hi:

While I would probably do this in perl of awk (it would be much faster), you can do it within the shell:

Your:
DAT=`printf "%.378s\n" $LIN`
echo $DAT >> test

becomes:

DAT=$(printf '%-378.378s' "${LIN}")
echo "${DAT}" >> test

Note that echo does an implicit linefeed so that no LF is needed in the printf. The quotes around ${DAT} are required to include the white space. I have also replaced your backticks with the more modern syntax $(command)

Regards, Clay

If it ain't broke, I can fix that.

Re: Formating output to fixed lenght

Does this work?

rm -f test
cat *82.txt | while read LIN
do
printf "%378.378s\n" $LIN >> test
done
tr -d "[\015\012]" < test > testout
exit


HTH

Duncan

I am an HPE Employee
Accept or Kudo
Steve Lewis
Honored Contributor

Re: Formating output to fixed lenght

Sachin gave you half the answer.

First sed the file to append a load of spaces to the end of each line, to guarantee more than 378 chars, then use your printf routine to chop everything back to 378 chars afterwards.

$ is EOL in sed. If you replace it with spaces it just adds the spaces before the EOL.

sed 's/$/ loads of spaces on the end/' infile > outfile

...printf "%.378s\n" $LIN` etc as you did before.



John Carr_2
Honored Contributor

Re: Formating output to fixed lenght

Hi

here is a script of mine which formats the output of bdf while changing the values to MB's. The principle for formatting can be applied to your file.

cheers
John.
John Carr_2
Honored Contributor
Solution

Re: Formating output to fixed lenght

Hi

here is a script of mine which formats the output of bdf while changing the values to MB's. The principle for formatting can be applied to your file.

cheers
John.

and again with the attachment
John Palmer
Honored Contributor

Re: Formating output to fixed lenght

Try this...

rm testout
...
do
printf "%-378s" ${LIN} >> testout
done
exit

No need for command substitution - printf direct to the file.

No need to remove the linefeed because printf hasn't written one.

Regards,
John
Victor Pavon
Advisor

Re: Formating output to fixed lenght

Ok Clay, I bite. How would the rest of the world awk this?

Your solution works, great answer BTW!!!

Victor