Ubuntu – Search output of AWK in another file

awkcommand linetext processing

I have two files fileA and fileB.

I have to extract column1 from fileA like awk '{print $1}' and then the output will be searched into other fileB and will save the matched records into a new file fileC in simple words like:

fileA:

seg1     rec1
seg2     rec2
seg3     rec3 

I need to retrieve column 1 by using awk command and this column 1 is searched into fileB to retrieve the records like:

fileB:

seg1     one
seg2     two
seg3     three
seg4     four
seg5     five

From fileA, column1 data is extracted and
and this data is used to search in fileB and matched record is saved to a test file.
My output should be like this:

fileC:

seg1       one
seg2       two
seg3       three

Best Answer

Can be achieved easily with awk as follows:

awk 'NR==FNR{inFileA[$1]; next} ($1 in inFileA)' fileA fileB > write_to_fileC

result,

seg1       one
seg2       two
seg3       three

at above, first we are reading the fileA and holds the entire column1 from into an array named inFileA, then look in fileB for its first column and if it's matched with the saved column1 from fileA then goes to print entire row of fileB.