Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如果第3列中的值位于另一个文本文件中,则删除行_Bash_Unix - Fatal编程技术网

Bash 如果第3列中的值位于另一个文本文件中,则删除行

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

我有一个长文本文件(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 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解决方案按列过滤(请参阅)。

使用awk:

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的数据后面有很长的数字,这可能是误报。