Awk solution

Hello! Well, I searched and wasn't able to find a specific example of my dilemma, so hopefully someone could assist? Or maybe there was an example but I missed it?

I have two files:

file1 = order data file
file2 = list of 65,000+ order numbers

I would like to extract from 'file1' any records which match the numbers in 'file2'.

I am able to extract individual or even multiple numbers from 'file1' by using the following syntax:

awk ' BEGIN{
while (getline i < "file1"){

               orderno=substr\(i,4,8\)

if ( orderno == "12345678"){ print i > "outfile"}

                                               \}
             \}' 

...but I am unsure how to use the contents of 'file2' as my list of order numbers to extract from 'file1'.

Any ideas?

Thanks very much in advance for any assitance provided.

Please give us the first 3 - 4 lines from each file, dummy data is fine

Thanks for the quick reply. Here are examples from the two files:

file1 contents:

76501792245HSM0000000000000000 06866BET
76501792783HSM0000000000000000 00763ELM
76501805678HSO0000000000000000 30102WES
76501894712HSO0000000000000000 70178PSI
76501895388HOM0001028700000000 06133APP
76501937632HSO0000000000000000 00225ACK

file2 contents:

01792780
01792783
01894712
01894713
01897825
01900407

Per the examples, two records from file1 should be printed to output. (order numbers 01792783 and 01894712).

Does it have to be in awk(1)?

Here is a shell script that would do the trick:

$ cat file_order_number_test.sh
#!/bin/bash
while read ORDERNUMBER; do
  grep "^${ORDERNUMBER}" file1-order-data-file
done < file2-order-numbers
$

Here are example data files:

$ cat file1-order-data-file 
number, cust., part, qty, date 
0001, BAE Systems, wing, 1, 20th Dec. 2000
0002, BA, In Flight Lunches, 20, 21st Dec. 2000
0003, Boots The Chemist, Aspirin 500mg, 1000, 22nd Dec. 2000
0004, Cadbury, Full Fat Milk, 10000, 23rd Dec. 2000
0005, Woolworths, A4 Photocopy Paper, 144, 27th Dec. 2000
0006, Marks and Spencer, Cotton Y-Fronts, 2000, 28th Dec. 2000
0007, Goldburg Jewellers, Gold Bullion, 10, 29th Dec. 2000
$
$ cat file2-order-numbers 
0001
0003
0005
0007

Here is a test run:

$ ./file_order_number_test.sh
0001, BAE Systems, wing, 1, 20th Dec. 2000
0003, Boots The Chemist, Aspirin 500mg, 1000, 22nd Dec. 2000
0005, Woolworths, A4 Photocopy Paper, 144, 27th Dec. 2000
0007, Goldburg Jewellers, Gold Bullion, 10, 29th Dec. 2000

Is that any good to you?

nawk '
  FNR==NR {f2[$1];next}
  (s=substr($0,4,8)) in f2 {print s}
' file2 file1
grep -f file2 file1