Computing.Net > Forums > Programming > awk multiple file searching

awk multiple file searching

Reply to Message Icon

Original Message
Name: moedriver
Date: October 13, 2004 at 11:56:18 Pacific
Subject: awk multiple file searching
OS: XP
CPU/Ram: 512
Comment:

hi,
i would like some help towards an awk script that can do the following:

take $1 for each record from file1 and compare it with all the $1 fields in file2. Once a match is found to print the $2 of file 2 and $2 of file1 for that match. So for example:

file1 contains
3 10.1
4 10.2
5 10.3
6 10.4

file2 contains
4 11.1
3 11.2
6 11.3
5 11.4

so the first field,$1 of file1 (3) is stored and looked for in file2. once found (i.e. the second entry in file2) then prints $2(of file2), $2(of file1) to another file (say, resultfile). e.g. for the $1 field of the first line of file1 (3) searched, it produces:

11.2 10.1

can someone plz help me...i have tried some scripts, but using mutliple files for seaches and linking is proving to be a bit difficult...thanks a lot
moe



Report Offensive Message For Removal

Response Number 1
Name: Wolfbone
Date: October 13, 2004 at 13:02:54 Pacific
Subject: awk multiple file searching
Reply: (edit)

awk 'BEGIN {while (getline < "file2") { l[i]=$0 ; i++ } } ; { for (j=0 ; j<=i ; j++) { split(l[j],f) ; if (f[1] == $1) print f[2],$2} }' file1


Report Offensive Follow Up For Removal

Response Number 2
Name: moedriver
Date: October 13, 2004 at 16:24:50 Pacific
Subject: awk multiple file searching
Reply: (edit)

hi Wolfbone,
thanks for taking time and replying to the problem with the correct solution, i appreciate it...

PS- i will be working with data trace files using awk and may require your help (again) with any difficulties....thanks once again
moe


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: awk multiple file searching

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software