M.sh
M.sh

Reputation: 167

Take input from a column of one file, and match a condition in two columns in second file

I have two files like this:

File 1:

1   1987969 1987970 .   7.078307    33
1   2066715 2066716 .   7.426998    34
1   2066774 2066775 .   6.851217    33

File 2:

1   HANASAI gelliu  1186928 1441229
1   FEBRUCA sepaca  3455487 3608150

I want to take each value of column 3 in File 1, and search in File 2 with a condition like (if File1_col3_value >= File2_col4_value && File1_col3_value <= File2_col5_value) then print the whole line of File 2 in a new file.

One more thing is also important: for every variable from file_1 to be searched in file_2, the corresponding value in column_1 should be the same in both files, e.g. for '1987970' of file_1, the value in corresponding in column_1 is '1', so in file_2, it should also be '1' in first column.

Thanks

Upvotes: 0

Views: 270

Answers (1)

jas
jas

Reputation: 10865

EDIT: Only considers lines with matching "class" values in column 1

$ cat msh.awk
# Save all the pairs of class and third-column values from file1
NR==FNR { a[$1,$3]; next }

# For each line of file2, if there exists a third-column-file1
# value between the values of columns 4 and 5 in a record of the
# same class, print the line
{
    for (cv in a) {
        split(cv, class_val, SUBSEP);
        c = class_val[1];
        v = class_val[2];
        if (c == $1 && v >= $4 && v <= $5) {
            print
            break
        }
    }
}

$ cat file1
1   1987969 1987970 .   7.078307    33
1   2066715 2066716 .   7.426998    34
1   2066774 1200000 .   6.851217    33
1   2066774 2066775 .   6.851217    33

$ cat file2
1   HANASAI gelliu  1186928 1441229
1   FEBRUCA sepaca  3455487 3608150

$ awk -f msh.awk file1 file2
1   HANASAI gelliu  1186928 1441229

Upvotes: 2

Related Questions