Retreive the records from file2 by using the first field in file1

Hi Freinds,

i have a file1 as below
file1

 
1|ndmf|fdd|d3484|34874
2|jdehf|wru7|478|w489
3|dfkj|wej|484|49894

file2 contains lakhs of records and not in sorted order
i want to retrive only the records from file2 by searcing the first field of file 1

i used

 
grep ^1 file2 >out.txt
grep ^2 file2 >>out.txt
grep ^3 file2>>out.txt

this i need to do 3 times . if i have some 1L records in file1 and need to take only matching records , i am struck

Please help me i am newbie to shell

Try..

IFS="|"
while read x y
do
grep "^$x" file2 >> out.txt
done<file
1 Like

Assuming that file2 is also a pipe-separated file and you wish to match the first fields of the 2 files (i.e., 1 will match only 1 and not 12,132,etc.), try:

awk 'FNR==NR{a[$1];next}$1 in a' FS=\| file1 file2 > out.txt

@pamu
You don't need to open out.txt multiple times in the loop. Just keep it open for the duration of the loop. That'll be more efficient.

while IFS='|' read x y
do
grep "^$x|" file2
done < file1 > out.txt
1 Like

Better than running grep 1L times is to prepare a "pattern file" and use grep with it:

awk -F"\|" '{print $1}' file1 > pattfile; grep -f pattfile file2 >out.txt; rm pattfile
1 Like

@pamu.Elixir,Rudic : Thaks for you reply and help. Appreciate your help :slight_smile: it is working as expected :slight_smile: