-1

I went through the answers in the following question but I couldn't get expected output as shown below.

comparing the first column of two files and printing the entire row of the second file if the first columns match

File 1

CLASS|124655
CLASS|124656
CLASS|124657
CLASS|124658

File 2

CLASS|124655|STEVE|SMITH||
CLASS|124656|ROGERS|KNIGHT||
CLASS|124657|MITCHELL|BRADFORD||
CLASS|124657||||1236536
CLASS|124658|NORMAN|JONES||
CLASS|124658||||1236539
CLASS|124665|JEFF|JONES||
CLASS|124665||||1236556

Expected output

CLASS|124655|STEVE|SMITH||
CLASS|124656|ROGERS|KNIGHT||
CLASS|124657|MITCHELL|BRADFORD||
CLASS|124657||||1236536
CLASS|124658|NORMAN|JONES||
CLASS|124658||||1236539

I tried grep -f file1 file2 but it got killed by the Kernel and it was taking too long to process. File 1 and File 2 have more than 1m records.

1

An awksolution would be this:

$ awk -v FS="|" 'NR==FNR {get[$1"|"$2]++; next} get[$1"|"$2]' f1.txt f2.txt

Don't know if this works for 1m records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.