I'm trying to merge two files and make a third file.
However, two of the columns need to match exactly in both files AND I want everything from both files in the output if the two columns match in that row.
First file looks like this:
chr1 10001980 T A
Second file looks like this:
1 chr1 41980 41981 snp A G dbsnp.86:rs806721
I need column 1 and 2 in file 1 to match column 2 and 4 in file 2, respectively.
Any help you can provide is very much appreciated.
I've tried using the join function and awk, but I've failed miserably at both.
A match can exist in any row... the first file should ALL match in some way to an entry in the second file, but the second file has thousands of rows that will not match to the first file.
I'm working in bash on a unix device (macbook) in terminal.
This may not be the most efficient way if you're files are really large but it works. Let me know if any of it is unclear.
Code:
#!/bin/bash
while read line
do
col1=$(echo $line | awk '{print $1}')
col2=$(echo $line | awk '{print $2}')
row_from_file2=$(grep " $col1 " file2 | grep " $col2 ")
file2_col2=$(echo $row_from_file2 | awk '{print $2}')
file2_col4=$(echo $row_from_file2 | awk '{print $4}')
if [[ "$col1" = "$file2_col2" && "$col2" = "$file2_col4" ]]
then
echo "Found a match"
echo "$line $row_from_file2" >> file3
fi
done < file1
Output:
# cat file1
chr1 10001980 T A
# cat file2
1 chr1 41980 41981 snp A G dbsnp.86:rs806721
1 chr1 41980 10001980 snp A G dbsnp.86:rs806721
# ./match_file1_file2.bash
Found a match
# cat file3
chr1 10001980 T A 1 chr1 41980 10001980 snp A G dbsnp.86:rs806721
Yep. "file1", "file2" were all local to my working directory. I'm a big fan of using full paths in my scripts so I would suggest it. I just wrote everything local to be just get the logic working. Happy scripting.
the implementation is based on your original description of the key fields:
The sample files you provided don't have matching records based on your description. So either the description is wrong or the provided sample files are not representative.
Provide either a better description OR a more representative data files (using code tags).
Also provide a desired result based on the sample data files: either WITH the matching records OR withOUT one - we can only guess what the OP's intent might be...