comparing two fields from two different files in AWK
Hi,
I have two files formatted as following:
File 1: (user_num_ID , realID) (the NR here is 41671)
File 2: (realID , clusterNumber) (total NR here is 1000)
and i want to compare these two files based on File 1 $2 field and File 2 $1 field and get a resulting file like
File 3: (user_num_ID, realID, clusterNumber)
just to summarize: Create a new file3
Find where file2 ($1) is equal to file1($2)
and print to file3 file1($1) and file2($1,$2) (as a single record)
since im new in awk, i've tried several suggestions from previous threads but it seems that im doing something wrong...
Thank you for your quick response. I've tried the script you suggested and it seems that i get an empty file3...is there an issue on whether i'm using awk under cygwin? I'm sure that i have types exactly what you suggested.
Thanks again
ps: i've tried using grep -f by just comparing the real_custID of file2 with file1 and it worked fine..but i'm curious on how this can be done using awk.
I roughly understand that this code first reads in a smaller file(file_2) and stores every line into an array f2.
Next, when it reads in the other bigger one, file_1, line by line, it checks each line's 2nd field against the array f2. If there is a match, it prints out the current line in process and an element from the array f2.
Use the 1st line of file_2 as an example, "fcust_034_60 2"
==> f2[$1]=$2
It looks to me, the code only put the 2nd field, which is the number 2, into the array's position $1, not the entire current line goes in there.
2. how f2's element being retrieved by the code
==> f2[$2]?
If it had just put a field in the 1st step,
how could the code use "fcust_034_60" now read in from file_1 to search against the array f2?
To me, "fcust_034_60" is even not in f2.
And in the previous step, seems to me, the code "f2[$1]=$2", just put $2 from the line in the $1 position of f2. How come when retrieved, it is from the position $2 of f2?
I am new here and awk is new to me too.
The book I have at hand is talking about using awk to process one single file only.
Using awk to process 2 or more files at a time seems will take me some get used to it.
Thanks to the other person here on the other page clarified the NR/FNR issue very well,
Now I have a vague impression about how awk uses NR/FNR to handle 2 files's comparison at a time.
Hopefully someone here will give me some hints to my questions above too. Thank you in advance.
Dear All,
I want to compare File1 and File2 (Separated by spaces) using four fields (Column 1,2,4,5).
Logic: If column 1 and 2 of File1 and File2 match exactly and if the File2 has the same characters as any of the characters present in column 4 and 5 of file1 then those lines of file1 and file2... (6 Replies)
I want to compare File1 and File2 (Separated by spaces) using four fields (Column 1,2,4,5).
Logic: If column 1 and 2 of File1 and File2 match exactly and if the File2 has the same characters as any of the characters present in column 4 and 5 of file1 then those lines of file1 and file2 are... (1 Reply)
Hi All,
I am looking for an awk script to do the following
Join the fields together only if the first 4 fields are same.
Can it be done with join function in awk??
a,b,c,d,8,,,
a,b,c,d,,7,,
a,b,c,d,,,9,
a,b,p,e,8,,,
a.b,p,e,,9,,
a,b,p,z,,,,9
a,b,p,z,,8,,
desired output:
... (1 Reply)
Hi
I have 2 files as below
File 1
Chr Start End
chr1 120 130
chr1 140 150
chr2 130 140
File2
Chr Start End Value
chr1 121 128 ABC
chr1 144 149 XYZ
chr2 120 129 PQR
I would like to compare these files using awk; specifically if column 1 of file1 is equal to column 1 of file2... (7 Replies)
I am newbie to unix and would please like some help to solve the task below
I have two files, file_a.text and file_b.text that I want to evaluate.
file_a.text
1698.74
1711.88
6576.25
899.41
3205.63
4187.98
697.35
1551.83 ... (3 Replies)
I've 2 files. Need to compare File1.Field1,File1.Field2 with File2.Field1,File2.Field2. If matches then create a new file.
File1
10 A|ADB|967143.24|1006101.5
3E HK|DHB|24294.76|242513.89
ABN ACU|ADB|22104.69|51647.14
ABN BU|DBA|39137.14|109128.38
ABN|ADB|64466.89|167936.55
ABOC... (2 Replies)
Hi all,
I searched the forum and tried to learn from the similar posts. However, I am new and I need to get help on this. I hope an expert kindly help me to sort this out.
I need to compare field 1 and 2 of the first file with the same fields of the second file and if both fields matches... (9 Replies)
Hi,
i want to compare two files by one field say $3 in file1 needs to compare with $2 in file2.
sample file1 - reqd_charge_code
2263881188,24570896,439
2263881964,24339077,439
2263883220,22619162,228
2263884224,24631840,442
2263884246,22612161,442
sample file2 - rg_j
... (2 Replies)
I have two files with ids and email addresses. File 2 cotains a subset of the records in file 1. The key field is the first field containing the id.
file 1:
123|myadr@abc.com
456|myadr2@abc.com
789|myadr3@abc.com
file 2:
456|adr456@xyz.com
Where the record appears in the second... (3 Replies)
Hi Experts,
I need your timely help. I have a problem with merging two files. Here my situation :
Here I have to compare first three fields from FILE1 with FILE2. If they are equal, I have to append the remaining values from FILE2 with FILE1 to create the output.
FILE1:
Class ... (3 Replies)