For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2004 > Two input files and program









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Two input files and program
Micha³

2004-11-16, 6:50 pm

Hi I've just started to learn awk and I have a question. I can write very
simple scripts but this task I think is too difficult for me.
I have two input files
First contains data as follows separated with " "
1 1
1.12 2
1.13 4
1.24 5
and second file contains similar data :
1.02 1
1.13 2
1.20 3
1.28 5
This files are very big and I want to:
Compare fields in the second columns and if fields are equal I want awk to
substr field from first column of second file with first column of first
file.
So this script should do. Get first field in second column in second file,
search for the same field in second column first file and if they are equal
substr fields in first columns.If they aren't equal search in next field
For this example I gave awk should produce:
0.2 1
0.01 2
0.04 5

Tkanks for any answers


Ed Morton

2004-11-16, 6:50 pm



Micha³ wrote:

> Hi I've just started to learn awk and I have a question. I can write very
> simple scripts but this task I think is too difficult for me.
> I have two input files
> First contains data as follows separated with " "
> 1 1
> 1.12 2
> 1.13 4
> 1.24 5
> and second file contains similar data :
> 1.02 1
> 1.13 2
> 1.20 3
> 1.28 5
> This files are very big and I want to:
> Compare fields in the second columns and if fields are equal I want awk to
> substr


ITYM "subtract" not "substr".

field from first column of second file with first column of first
> file.
> So this script should do. Get first field in second column in second file,


ITYM Get the second field in the first line in the second file.

> search for the same field in second column first file and if they are equal
> substr


ITYM "subtract" not "substr".

fields in first columns.If they aren't equal search in next field

ITYM If ther aren't equal search in the next line

> For this example I gave awk should produce:
> 0.2 1
> 0.01 2
> 0.04 5
>
> Tkanks for any answers


I assume the values in the second column of file2 are unique and that if
they aren't unique in file1 you want the same operation applied to all.

If all my assumptions above are correct, then this should so it (untested):

awk 'NR==FNR{f1[$2]=$1;next}
$2 in f1{$1=$1-f1[$2];print}' file2 file1

Regards,

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com