Checking number prefix

I have some trouble in my script.

I am currently using:

 awk '{anum=substr($1,3,22); sub(/^0+/, "", anum); print anum}' file1 | grep -nf file2 | cut -d: -f1 | awk 'FNR==NR{a[$1];next};FNR in a' - file1

file1

 5000000000009855892590xxxx xxx
 5000000000000068582654xxxx xxx
 5000000000009855892580xxxx xxx
 5000000000000765432100xxxx xxx

file2

 9855892588
 985589259
 8265
 76543210

I am getting the output using the two files below (file1 and file2):

 5000000000009855892590xxxx xxx
 5000000000000068582654xxxx xxx
 5000000000000765432100xxxx xxx

But my expected output is just:

5000000000009855892590xxxx xxx
5000000000000765432100xxxx xxx

My problem is that it captures 8265 in the middle of 5000000000000068582654xxxx which is wrong. What else can I use in replacement of grep -nf to meet my condition? Should the numbers in file2 match the prefix or whole number of 3rd to 22nd digit of file1 (w/o leading zeros).


Source: unix

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.