comparing two files and getting result

March 10, 2010 at 23:53:04
Specs: Linux i686
hi guys!!
i have two files..file1 with data
111
222
333
444
and file2 with data
111,home
222,hotel
333,caffetaria
444,pool
555,restuarant
i want to know script that compares file1 with file2 for only first feild and give me result as
111,home
222,hotel
333,caffetaria
am new to linux script!!
can any one plz help!!

See More: comparing two files and getting result

Report •

#1
March 11, 2010 at 19:33:26
Actually, based on your data the result should be:

111,home
222,hotel
333,caffetaria
444,pool


One way is to read the first datafile into an associative array and then read the other file and see if field 1 is in the array:

:#!/bin/bash

awk ' BEGIN { FS=","
   while ( getline < "datafile1" > 0 )
      myarr[$0]=$0
}
{
if($1 in myarr)
   print $0

} ' datafile2



Report •
Related Solutions


Ask Question