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

awk able to compare a file line by line? 2

Status
Not open for further replies.

wellster34

Programmer
Sep 4, 2001
113
CA
Hi,

I have a file:

10001|100.00|SOURCE1|
10001|100.00|SOURCE2|
10002|500.00|SOURCE1|
10002|1000.00|SOURCE2|

I need to compare the lines by reading them one by one and seeing if they differ in the amount fields by using the primary key as the uniqueness (first column). The SOURCE1 and SOURCE2 at the end indicate what source they are from. So basically, by using the primary key and comparing the numbers between the two sources. I need to report on any differences...

i.e. compare the following
10001|100.00|SOURCE1|

against
10001|100.00|SOURCE2|

to see if the numbers match. Since they match, no reporting required.

If they differ like:
10002|500.00|SOURCE1|
10002|1000.00|SOURCE2|

I need to report on both lines...


Is this possible in AWK? I was checking to see if I can store the values in variables but they would get overwritten when I read the next record... Any ideas?

Thanks for your time!



 
If you had SOURCE1 and SOURCE2 as two text files (with all the same index keys, etc etc), you could just use the 'diff' tool to check for differences.

-Haben sie fosforos?
-No tiengo caballero, but I have un briquet.
 
A starting point:
awk -F'|' '
$1 in a{if($2!=a[$1])print $1,$2,$3,",",a[$1],b[$1];next}
{a[$1]=$2;b[$1]=$3}
' /path/to/input


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The SOURCE1 & 2 are in different files with the same key and etc... I just tried the diff command and the results are hard to understand.
 
thanks for your help! I understand it know... delayed reaction. lol Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top