'extract line if value in first column matches and value in second column not matches in another file in bash
I have 2 different files that look like this
61435320 rs10000085
12984967 rs10000091
32039123 rs10000150
and this:
61435320 rs12958
12984967 rs10000091
32039123 rs37892
I would like to extract only those rows which match the first field but do NOT match the second field.
So, my desired output would look like (I only want the first field):
61435320
32039123
I've tried this answer on Ask Ubuntu but, unfortunately, it is not working for me (I guess it is not exactly what I'm looking for).
Solution 1:[1]
Suppose your files are called a
and b
, you can do it like this:
join a b | awk '{if ($2!=$3){print $1}}'
If your files are not sorted properly, join may have issues. You can sort them like this:
join <(sort a) <(sort b) | awk '{if ($2!=$3){print $1}}'
Solution 2:[2]
Only with awk :
awk -v'f=file2' '{if((getline b<f)>0)$0=$0 b}$1==$3 && $2 != $4{print $1}' file1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | fancyPants |
Solution 2 | ctac_ |