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

printing amounts gt 200.00

Status
Not open for further replies.

ng1

Technical User
Aug 22, 2007
39
US
I have a file with 5 fields. These fields are separated by a colon. The 5th field is the field with the amount. I want to find amounts less than 200.00 and print them out.

I try this:

awk -F: '$5 < 200{print $5}' filename

It works except for amounts that are over 1,000.00. These amounts are seen as less than 200.00 for some reason. I am sure it has to do with the comma. I don't know why though because my field separator is a colon.
 
Hi

ng1 said:
I am sure it has to do with the comma. I don't know why though because my field separator is a colon.
Try it. See how those values looks as decimal and as float :
Code:
[blue]master #[/blue] echo '[green]1000.00[/green]
[red]1,000.00[/red] ' | awk '{printf "the %s as decimal : %d , as float : %f\n",$1,$1,$1}'
the [green]1000.00[/green] as decimal : [green]1000[/green] , as float : [green]1000.000000[/green]
the [red]1,000.00[/red] as decimal : [red]1[/red] , as float : [red]1.000000[/red]
The comma ( , ) can not be part of a number. When the field is evaluated as numeric, the parsing breaks at that the first invalid character, in this case the comma.

So just remove the commas :
Code:
awk -F: '[red]{gsub(/,/,"",$5)}[/red] $5 < 200{print $5}' filename

Feherke.
 
It removes the comma from the numbers that are 1,000 or greater and shifts their position to the left one digit. It still prints those numbers though.


100.00
3328.00
2112.00
1596.00
120.00
150.00
150.00
 
I edited the file (took out the comma with the editor I use) before doing the awk command and it worked.
 
And what about this ?
awk -F: '{gsub(/,/,"",$5)} $5[!]+0[/!] < 200{print $5}' filename

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top