有點冗長,也許吧,但是如果你需要操作(排序,搜索等)內容,將會允許靈活性。通常,如果數據文件適合內存,我更願意將整個文件填充到內存中並從那裏進行。
use strict;
use warnings;
# initialize the hash to contain the content
my %bed;
# read the entire file into memory
# stuffing same into a hash
while(<DATA>)
{
chomp;
my @line = split;
my $car = $line[0];
my $cdr = join(' ', @line[1,-1]);
push(@{$bed{$car}}, $cdr);
}
foreach my $k (keys %bed)
{
# create filename
my $fn = $k . '.txt';
# open file for writing
open OUT, '>', $fn or die "Cannot open $fn, $!";
# print each element of the hash key
foreach my $e (@{$bed{$k}}) { print OUT "$e\n"; }
# close file`
close OUT;
}
exit(0);
__DATA__
chr1 12190 12227
chr1 12595 12721
chr2 876522 876688
chr2 887378 887521
而且,是的,這個awk是相當的,但更簡單:'awk'{f = $ 1「.bed」; print> f}'example.bed' –
非常感謝Glenn!它工作得很好。 – Renee