select records from one file based on a second file

Hi all:

I have two files:

file1:

74 DS  9871 199009871    1 1990  4  1 165200 Sc
pr de te sa ox
      1.0      1.0  13.0000  35.7560    5.950
      3.0      3.0  13.0100  35.7550    5.970 
**
74 DS 99004 74DS99004 6738 1990  4  1 165200 Eb
pr de te sa ox
      1.0      1.0  13.0000  35.7560    5.950
      3.0      3.0  13.0100  35.7550    5.970 
**
29 CS   563 198700563  280 1987  7 27 043900 Ic
pr de te sa ox
      9.0      8.9  12.2700  35.6500    6.060
     11.0     10.9  12.2100  35.6490    6.100
**
74 DS  9871 199009871    8 1990  4  3 161500 Sc
pr de te sa ox
      1.0      1.0  13.4500  35.7420    5.940
      3.0      3.0  13.4500  35.7420    5.950
**
74 DS 99004 74DS99004 6911 1990  4  6 042500 Eb
pr de te sa
      2.0      2.0  15.3300  35.5920
      3.0      3.0  15.3300  35.5920
**

and file2:

29 CS   563 198700563  280 1987  7 27 043900 Ic
74 DS 99004 74DS99004 6738 1990  4  1 165200 Eb
74 DS 99004 74DS99004 6911 1990  4  6 042500 Eb

What I need is to retain from file1 the full records pointed in file 2 (not need to be ordered) in such as:

74 DS 99004 74DS99004 6738 1990  4  1 165200 Eb
pr de te sa ox
      1.0      1.0  13.0000  35.7560    5.950
      3.0      3.0  13.0100  35.7550    5.970 
**
29 CS   563 198700563  280 1987  7 27 043900 Ic
pr de te sa ox
      9.0      8.9  12.2700  35.6500    6.060
     11.0     10.9  12.2100  35.6490    6.100
**
74 DS 99004 74DS99004 6911 1990  4  6 042500 Eb
pr de te sa
      2.0      2.0  15.3300  35.5920
      3.0      3.0  15.3300  35.5920
**

I have tried several approaches with awk oneliners, but didn't succeed because don't know how to deal with multiple files each with dissimilar ORS and FS. That is for file1 RS and ORS are the awk defaults whereas for file2:

{RS="\\*\\*\n+";ORS="**\n"}. 

Do you have a clue on how to proceed?

Thanks,

r.-

Try...

awk 'FNR==NR{a[$0]=$0;next}($0 in a),$0=="**"' file2 file1

Can you please explain your code, I always get confused while working with two files in awk.
Thanks,

Hi Rakesh,

FNR==NR{a[$0]=$0;next}

is used for storing the first file (here file2) content into an array "a" based on the key "$0" ( which is a complete row).

($0 in a),$0=="**"'

The above code is verifying whether each row from the file1 is present in the array or not ?.if present print it till the row equals to "**".

Ygor:

Please let me know if i am wrong somewhere.

That was absolutely brilliant! Thanks so much.

r.-

And this is because FNR is reset for each file read...right?

Sorry i forgot to mention :slight_smile: .Yes. "NR" will be keep on incrementing where as "FNR" will reset once a file processing is over and re start again for the next file.

and that will not let the first condition execute for the second file...
Thanks...great explaination.