Hi,
I'd like to intersect two files by the 4th col of the first file and 6th col of the second file. This is the code I use:
awk 'NR==FNR{A[$4];next}$6 File1 File2
However, this is only outputting the second file lines. I'd like to have both lines in a single line separated by a tab.
Thanks in advance
Something like this?
while read a1 a2 a3 a4 a5 a6 a7 <&1 && read b1 b2 b3 b4 b5 b6 b7 <&3
do
printf "%s\t%s\n" "$a4" "$b6"
done 1<File1 3<File2
Thanks for your response but this doesn't work for me. That returns me this error:
-sh: printf: write error: Bad file descriptor
BTW, my files are tab delimited
Try this...
paste <( awk '$0=$4' file1 ) <( awk '$0=$6' file2 )
And in the above code, try using echo -e instead of printf
--ahamed
Hi Ahamed,
it didn't work. Gives me this error:
-sh: syntax error near unexpected token `('
echo -e
doesn't work either
Which is your OS?
What is the error you are getting while you run MadeInGermany's code with echo?
I'm using Cygwin on windows
Paste the output of od -bc file1 and od -bc file2
--ahamed
The files are big, so I'm just pasting the first 5 lines:
File1
0000000 061 071 011 065 070 070 066 062 067 070 064 011 065 070 070 066
1 9 \t 5 8 8 6 2 7 8 4 \t 5 8 8 6
0000020 062 067 070 064 011 103 011 124 011 101 061 102 107 011 160 056
2 7 8 4 \t C \t T \t A 1 B G \t p .
0000040 101 062 071 065 124 011 145 064 066 141 065 144 061 071 055 062
File 2:
0000000 061 011 070 071 067 061 061 066 011 070 071 067 061 061 067 011
1 \t 8 9 7 1 1 6 \t 8 9 7 1 1 7 \t
0000020 107 011 101 011 113 114 110 114 061 067 011 160 056 105 061 065
G \t A \t K L H L 1 7 \t p . E 1 5
0000040 071 113 011 115 117 137 061 060 065 061 011 061 040 061 012 061
Alright, can you paste the exact command you have executed?
Please paste for both the codes.
od -bc TCGA_mut.bed | head -n +5
od -bc Met_mut.bed | head -n +5
"TCGA_mut.bed" is the first and "Met_mut.bed" is the second file.
I meant the paste command and the one given by MadeInGermany with output.
Oh sorry.
paste <(awk '$0=$4' TCGA_mut.bed) <(awk '$0=$6' Met_mut.bed)
while read a1 a2 a3 a4 a5 a6 a7 <&1 && read b1 b2 b3 b4 b5 b6 b7 <&3; do printf "%s\t%s\n" "$a4" "$b6"; done 1<TCGA_mut.bed 3<Met_mut.bed
Try this
while read a1 a2 a3 a4 a5 a6 a7 <&3 && read b1 b2 b3 b4 b5 b6 b7 <&4
do
echo -e "${a4}\t${b6}"
done 3<TCGA_mut.bed 4<Met_mut.bed
I'm a bit confused. I want to intersect two files based on their specific column, let's say column $4 from both files. This is just printing col 4 & 6 of the first and second files, respectively. This doesn't do the intersection.
Give us an sample input and expected output.
You mean coulmn 4 in file1 == column 6 in file2 then print?
Yes, that's what I meant.
Let's say this is my first file:
a b c d e f
And this is the second one:
1 2 3 4 5 d
This is what I want:
a b c d e f 1 2 3 4 5 d
awk 'NR==FNR{A[$4]=$0; next} $6 in A{ print A[$6]"\t"$0 }' file1 file2
Great, thanks for your help
---------- Post updated at 07:32 PM ---------- Previous update was at 05:55 PM ----------
Sorry Ahamad to bug you again but I have one more request. This scripts excludes the lines in the first file which don't intersect. I like to print all of the lines in the first file including the ones which don't intersect. Let me make it simple by this example:
File1
a b c d e f
g h i j k l
File2
1 2 3 4 5 d
Output:
a b c d e f 1 2 3 4 5 d
g h i j k l
Please let me know if this doesn't make sense to you.
---------- Post updated at 07:44 PM ---------- Previous update was at 07:32 PM ----------
This actually prints the all first file first then it prints the intersect. This will cause a lot of duplicates in my final output.