Problem with the awk in searching the contents in a file

Hello all,

I am a newbie in awk. I am struggling in this problem for a long.Actually I have two files, filea and fileb. File a is actually a search key through it I have to find the corresponding japanese tag from file b.

filea contains the data like this:

sm982882  sm1893548
sm2420025 sm2420128
sm2420025 sm1654558
sm1715919 sm3504435
sm1050729 sm1049371
sm1200230 sm1783839

and fileb contains the data like this:

    147    sm1864839;sm1878115;sm1950540;sm2106734;sm2178096;sm2183582;sm2415525;sm982882;sm2512481;sm2554063;sm2652509;sm1893548;sm2723097;sm2785830;sm2898082;
    513    sm479;sm36902;sm41429;sm43160;sm68724;sm72831;sm1654558;sm153858;sm155799;sm157569;sm162299;sm168973;sm171423;sm174780;sm176263;sm181722;sm185763;sm210482;sm215266;sm218312;sm224020;sm232251;

The result of output should be the like this:

  sm982882  sm1893548

I use the following way but it does not produce any result:

awk ' {col1=$1; col2=$2;} END{if($3~/col1/&&/col2/) print $1 col1,col2;}' filea fileb

.

Any help in this will be highly appreciable.

Best,
Mohan:wall:

Hi, you were on your way, but you need to distinguish the phases in which you are processing filea or fileb. When NR is equal to FNR, awk is processing the first file which in this case is fileb. There is no input processing in the END section.

awk 'NR==FNR{A[$3]=$1;next}{for(i in A)if(i~$1"\;" && i~$2"\;") print A,$0 }' fileb filea
 sm982882  sm1893548
1 Like
# awk -F'[ ;]' 'NR==FNR{a[$1]=$2;next}{for(i=1;i<=NF;i++)if(($i in a))for(j in a){print $1,j,a[j];next}}' filea fileb
 sm982882 sm1893548

regards
ygemici

1 Like

Thank you both Scrutinizer and ygemici, my problem is solved :slight_smile: