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

Read Input from 1st File to Locate Record in 2nd File 2

Status
Not open for further replies.

thunderkid

Technical User
Oct 26, 2000
54
0
0
US
I have tried to adapt other thread solution to tackle this problem but have not been able to get any to work. I would like get awk program to read input from 1st file and identify any record that matches the input in the 2nd file. See below for sample file format:

1st file:

196
197
234

2nd file:

196 aaaa bbbb cccc
197 abcc dfgg ddsee
198 hhhf fkdfk fdsdd
200 dfas kdfkd dfsds
234 kdfle fdad dafd

Thanks in advance for your assistance!

Thunderkid
 
Thunderkid:

Not much fun, and strictly brute force:

file.1
196
197
234

file.2
196 aaaa bbbb cccc
197 abcc dfgg ddsee
198 hhhf fkdfk fdsdd
200 dfas kdfkd dfsds
234 kdfle fdad dafd

Regards,


Ed

#!/bin/ksh

while read line; do # read first file
while read line2; do # read second file
ret_line=$(echo $line2|awk ' { print $1 } ')
if [ "$line" == "$ret_line" ]
then
echo $line2
fi
done < file.2
done < file.1
 
Ed,
I have tried your script using the following command:
script file.1 file.2

I am getting the following error message (total of 18 times):

script[6]: test: ] missing
script[6]: test: ] missing
script[6]: test: ] missing
script[6]: test: argument expected


Any hint as to what is my problem, besides being a novice at awk.

Thunderkid
 
Hi,
Here is a(90%)awk way to do it even though it too
is ugly.

gawk -v src=&quot;filename1&quot;' BEGIN {
while ((getline arr[a++] < src) > 0) {
#do_nada or print a, or filter the input.
}
close(src)
}
{
for (xx in arr) {
if (arr[xx] == $0) {
print arr[xx], &quot;matches&quot;, $0
}
}
}' filename2 | sort -u

Note: that this example is for gawk, don't know how any
other awk will behave.
 
Excuse me::reading your example again you would want to use either::
for (xx in arr) {
to_match = substr(arr[xx],1,3)
if (to_match == substr($0,1,3)) {
print $0
}
}

OR:
instead of if arr[xx] == $0,
use if arr[xx] ~ $0.

None of this is perfect and I am in a hurry, sorry.
 
Thunderkid:

I duplicated your error with:

if [ &quot;$line&quot; == &quot;$ret_line&quot;]
.
.

The shell if very particular about having a least 1 space around [ and ]:

if [ &quot;$line&quot; == &quot;$ret_line&quot; ] # need a space between
# double quote and bracket

Regards,


Ed



 
awk 'BEGIN{
while ((getline < &quot;file.1&quot;) > 0) { arr[ $1] = 1}
}
{
if( arr[$1] == 1) print

}' file.2 Gregor.Weertman@mailcity.com
 
Thanks to all who submitted responses. I was able to get Ed's awk script to work. I failed to consider the time it takes awk to execute the script. My file.1 is 20 data point. However, my file.2 is 13,000 records. It takes all morning to get through the 20 data items. Can anyone think of a faster way to match up the two files?

Thunderkid [sad]

 
Try either of the other scripts, they may be faster.
Olded's script is more processor intensive than either
of the pure awk scripts should be.
gregor's script in particular should be efficient.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top