我想通过删除具有相同值的列来帮助您修剪文件。
# the file I have (tab-delimited, millions of columns)
jack 1 5 9
john 3 5 0
lisa 4 5 7
# the file I want (remove the columns with the same value in all lines)
jack 1 9
john 3 0
lisa 4 7
你能给我任何关于这个问题的指导吗?我更喜欢 sed 或 awk 解决方案,或者可能是 perl 解决方案。
提前致谢。
最好的事物,
最佳答案
这是一个快速的 perl 脚本,用于确定可以剪切哪些列。
open FH, "file" or die $!;
my @baseline = split /\t/,<FH>; #snag the first row
my @linemap = 0..$#baseline; #list all equivalent columns (all of them)
while(<FH>) { #loop over the file
my @line = split /\t/;
@linemap = grep {$baseline[$_] eq $line[$_]} @linemap; #filter out any that aren't equal
}
print join " ", @linemap;
print "\n";
您可以使用上述许多建议来实际删除列。我最喜欢的可能是 cut 实现,部分原因是可以修改上面的 perl 脚本来为您提供精确的命令(甚至为您运行它)。
@linemap = map {$_+1} @linemap; #Cut is 1-index based
print "cut --complement -f ".join(",",@linemap)." file\n";
关于perl - 如何修剪文件 - 删除具有相同值的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6363583/