AWK - phone bill with several files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK - phone bill with several files
# 1  
Old 05-23-2011
Java AWK - phone bill with several files

First of all I want apologize for my English.Smilie
I have this problem, I would like to make a phone bill, the first file has Phone number, seconds of the call and call destination, and the second file has the prices for each destination call, and would like to have a third file where it calculate all.

Example:

(Phone number) (seconds of the call) (call destination)


file1.txt
666666661 158 ENG
666666662 175 AUS
666666663 357 PAR
666666664 741 MAD
666666665 624 MOS
666666661 847 MAD
666666662 147 AUS
666666662 582 PAR

-------------------------------------------------
(destination) (price)

file2.txt
ENG 0.02
AUS 0.12
PAR 0.03
MAD 0.06
MOS 0.21

--------------------------------------------
file3.txt
666666661 53.98
666666662 56.1
666666663 10.71
666666664 44.46
666666665 131.04


---------------------------------------
I'm tring this, but I only get one record per file. Smilie

Code:
#!/bin/bash

awk  '
BEGIN {


{while ( getline < "file1.txt" )
            {
        i++       
        f1[i] = $1
          }
      }

{while ( getline < "file2.txt" )
            {    
        x++       
        f2[x] = $1
          }
      }

}


{print [NR], [NR] }' file3.txt

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

I would appreciate any help.

Last edited by Godie; 05-23-2011 at 09:10 AM..
# 2  
Old 05-23-2011
Here you go
Code:
awk 'FNR==NR{tarif[$1]=$2;next}{cust[$1] += ($2 * tarif[$3])} END{ for (i in cust) print i,cust[i]}' file2.txt
666666661 53.98
666666662 56.1
666666663 10.71
666666664 44.46
666666665 131.04


Last edited by kumaran_5555; 05-23-2011 at 08:55 AM..
This User Gave Thanks to kumaran_5555 For This Post:
# 3  
Old 05-23-2011
kumaran_5555 missed out the 2nd argument 'file1.txt'
Code:
awk 'FNR==NR{tarif[$1]=$2;next}{cust[$1] += ($2 * tarif[$3])} END{ for (i in cust) print i,cust[i]}' file2.txt file1.txt
666666661 53.98
666666662 56.1
666666663 10.71
666666664 44.46
666666665 131.04

This may be a lot easier for Godie to understand
Code:
awk '

# process first file (file2.txt) to build up the lookup table
NR==FNR {
	tarif[$1]=$2
	next
}

# process all the records in the second file (file1.txt)
{
	cust[$1] += $2 * tarif[$3]
}

# output the total per phone number 
END{
	for ( i in cust ) {
		print i, cust[i]
	}
}' file2.txt file1.txt

This User Gave Thanks to chihung For This Post:
# 4  
Old 05-23-2011
kumaran and chihungt, thanks indeed, it was very helpful.

regards
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Solaris

Solaris View/Transfer Files Android Phone

I'm trying to sync/transfer files from my UNIX box and Android Phone and vice versa. I know that Android Phones show up seamlessly (mostly) in Linux given their incestuous relationship. Is there a way to do it in UNIX or more specifically in Solaris 11.3 (i86)?:confused: I haven't found one... (2 Replies)
Discussion started by: Nostradamus1973
2 Replies
Login or Register to Ask a Question