Bash 如果第3列中的值位于另一个文本文件中,则删除行
我有一个长文本文件(haploypes.txt),看起来像这样:Bash 如果第3列中的值位于另一个文本文件中,则删除行,bash,unix,Bash,Unix,我有一个长文本文件(haploypes.txt),看起来像这样: 19 rs541392352 55101281 A 0 0 ... 19 rs546022921 55106773 C T 0 ... 19 rs531959574 31298342 T 0 0 ... 55103603 55106773 55107854 55112489 还有一个简单的文本文件(positions.txt),如下所示: 19 rs541392352 55101281 A 0 0 ... 19 rs54602
19 rs541392352 55101281 A 0 0 ...
19 rs546022921 55106773 C T 0 ...
19 rs531959574 31298342 T 0 0 ...
55103603
55106773
55107854
55112489
还有一个简单的文本文件(positions.txt),如下所示:
19 rs541392352 55101281 A 0 0 ...
19 rs546022921 55106773 C T 0 ...
19 rs531959574 31298342 T 0 0 ...
55103603
55106773
55107854
55112489
如果要删除positions.txt中存在第三个字段的所有行,请获得以下输出:
19 rs541392352 55101281 A 0 0 ...
19 rs531959574 31298342 T 0 0 ...
我希望有人能帮忙。这应该可以:
$ grep -vwFf positions.txt haplotypes.txt
19 rs541392352 55101281 A 0 0 ...
19 rs531959574 31298342 T 0 0 ...
:从文件中读取模式-f positions.txt
:反转匹配项-v
:仅匹配完整单词(避免子字符串匹配)-w
:固定字符串匹配(不要将模式解释为正则表达式)-F
awk 'NR == FNR{a[$0] = 1;next}!a[$3]' positions.txt haplotypes.txt
细分:
NR == FNR { # If file is 'positions.txt'
a[$0] = 1 # Store line as key in associtive array 'a'
next # Skip next blocks
}
!a[$3] # Print if third column is not in the array 'a'
你试过什么吗?已经有许多类似的问题使用
awk
等进行了相同的操作。这与文件中任何位置的positions.txt
中的行不匹配。例如:a b c d e f g h i 55101281
而不仅仅是第三列。@andlrc确切地说-如果OP的数据后面有很长的数字,这可能是误报。