我有一个以空格分隔的大文件,包含数千行和列。我想删除除第一列外所有列都具有相同值的所有行。
输入:
CHROM 108 139 159 265 350 351
SNP1 -1 -1 -1 -1 -1 -1
SNP2 2 2 2 2 2 2
SNP3 0 0 0 -1 -1 -1
SNP4 1 1 1 1 1 1
SNP5 0 0 0 0 0 0所需
CHROM 108 139 159 265 350 351
SNP3 0 0 0 -1 -1 -1对于Panda (Delete duplicate rows with the same value in all columns in pandas)有一个类似的问题,我找到了一个部分解决方案,它删除了只包含零的行
awk 'NR > 1{s=0; for (i=3;i<=NF;i++) s+=$i; if (s!=0)print}' input > outfile但是,我想对数字1、0、1和2执行这一操作,并以标题和第1列作为标识符。
任何帮助都将不胜感激。
发布于 2018-10-09 14:54:37
我相信你可以这样做:
awk '{s=$0; gsub(FS $2,FS)} (NF > 1) {print s}' file其中产出:
CHROM 108 139 159 265 350 351
SNP3 0 0 0 -1 -1 -1这是怎么工作的?
{s=$0; gsub(FS $2,FS)}**:**此操作包含两个部分:- Store the current line in variable `s`
- Substitute in the current line `$0` all values of the second field including its starting field separator `FS` (`FS $2`) with a field separator `FS`. This has as a side effect the `$0` is redefined and all field variables and the total number of field `NF` are redefined. The field separator `FS` is needed to avoid matching `xx` if `$2=x`
(NF > 1) {print s}**:**如果你还剩一个以上的字段,打印这一行,它意味着你有不同的数字。发布于 2018-10-09 15:02:20
你能试一下吗。
awk '{val=$2;count=1;for(i=3;i<=NF;i++){if(val==$i){count++}};if(count!=(NF-1)){print}}' Input_file发布于 2018-10-09 15:03:00
你可以试试这个:
awk 'NR==1;NR>1{for(i=2;i<NF;i++)if($(i+1)!=$i) {print;next}}' file它打印标题行。
它遍历字段,直到找到与下一个字段之间的差异,然后打印出来,然后转到下一个字段。
https://stackoverflow.com/questions/52723920
复制相似问题