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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Numbers in a Script

Status
Not open for further replies.

gsdcrazy

Programmer
Sep 2, 2002
31
US
Gurus,

I know this is probably a very elementary question, but I am at a loss. I have read in fields from two files. The format of the fields are inconsistent. They consist of a sign (+ or -) and a number. One file has a consistent format with decimals (period followed by two numbers). The other file may have one or two numbers after the period or no period and decimal numbers at all. For example:

File 1 File2
Field 1 +000000002746.26 +000000002746.26
Field 2 +000000000117.1 +000000000117.10
Field 3 +000000001105 +000000001105.00

I need to both compare these values and write them to a file in a standard format (sign, number, period, two decimal numbers).

Is there a simple way to do this?

Thanks,
gsdcrazy

 
Try this:

[tt]#!/bin/ksh

paste file1 file2 | awk '
$1 != $2 { printf "%+.2f and %+.2f differ\n",$1,$2 ; next}
{ printf "%+.2f\n",$1 }
'[/tt]

It presumes that both files have the same number of lines in them.

paste puts the contents of file1 and file2 together in two columns. awk then prints out a message if they differ, or just prints out the value if they are equal. The "printf" statements convert the number into the format you wanted.

Annihilannic.
 
Annihilannic,

Thanks!! This code does exactly what I asked. Unfortunately, I did not ask it very well.

I can not match the two files directly. They are not the same format in number of records, spacing of each record, etc. Also, I need to take different action based on which fields do not match. I have successfully parsed the files to load the fields into individual UNIX script variables with the correct results. Is there some sort of formatting command that I can use against the individual UNIX script variables to turn values like +000000000117.1 into +000000000117.10 and +000000001105 into +000000001105.00 and leave +000000002746.26 alone? OR

The record in the file I need to format the fields looks like:

TRAILER0910200300021144:+000000002746.26:-000000001105.1:-00...and so on.

The "09102003" is the processing date. The "00021144" represents the number of records in an associated file. These are fixed positions. The remaining five fields are variable depending upon the number of decimal places (0, 1, 2). The ":" precedes the sign for each field "+" (positive) or "-" (negative). I don't know whether there is a way to modify the fields in the file and output a reformatted file or whether it would be easier to reformat the UNIX script variables that I parse the fields into in order to do the other validations and processing required.

I hope this makes some sense. I tried to apply the "printf" command to the UNIX variables, but keep getting syntax errors. The closest I came was:

TrailerTotalPremium1=awk `{ printf "%+.2f/n",`echo $trailer_data | cut -f2 -d:` } `;

But it doesn't work either.

Your help is greatly appreciated. Sorry to be such a pain.

Thanks,
gsdcrazy
 
Try something like this:
Code:
TrailerTotalPremium1=`echo "$trailer_data" | awk -F: '{ printf "%+.2f",$2}'`


Hope This Help
PH.
 
Convert numeric field to leading sign, zero-filled, width of 16 and precision of 2 .......

TrailerTotalPremium1=`printf "%+016.2f" $trailer_data`
 
PHV and Ygor,

Thanks so much for the help. I was getting a syntax error with the use of "awk", but the "printf" command from Ygor was exactly what I needed.

Again, Thanks,
gsdcrazy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top