免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 7896 | 回复: 15
打印 上一主题 下一主题

把一个文件根据其第一列值分割成多个小文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-17 08:08 |只看该作者 |倒序浏览
10可用积分
举例如下:
有一个文件内容为:
A||87767
A||98786
A||F9878
B||8789
B||90887
C||8U798

要根据第一列的值,分割成多个小文件
第一列有3种值,因此分割成3个小文件,分别为:
A.TXT:
A||87767
A||98786
A||F9878

B.TXT:
B||8789
B||90887

C.TXT:
C||8U798

同时想问一下,split函数是否无法把||作为分割符?
同时要考虑一下性能问题,因为要转换的文件都是百M级别的。

[ 本帖最后由 fikong2005 于 2009-12-17 09:20 编辑 ]

最佳答案

查看完整内容

[ 本帖最后由 cobrawgl 于 2009-12-18 13:01 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-12-17 08:08 |只看该作者
#!/usr/bin/perl


use strict;
use warnings;

use IO::File;

my %FH;

while (<DATA>) {
&nbsp;&nbsp;&nbsp;&nbsp;chomp;
&nbsp;&nbsp;&nbsp;&nbsp;my $name = (split //)[0];
&nbsp;&nbsp;&nbsp;&nbsp;$FH{$name}[0] = IO::File->new("> $name.txt") unless exists $FH{$name};
&nbsp;&nbsp;&nbsp;&nbsp;$FH{$name}[0]->print($_ . "\n");
&nbsp;&nbsp;&nbsp;&nbsp;$FH{$name}[1] += 1;
&nbsp;
}

for (keys %FH) {
&nbsp;&nbsp;&nbsp;&nbsp;$FH{$_}[0]->print('HDR' . $FH{$_}[1]) ;
&nbsp;&nbsp;&nbsp;&nbsp;$FH{$_}[0]->close;
}


__DATA__
A||87767
A||98786
A||F9878
B||8789
B||90887
C||8U798


[ 本帖最后由 cobrawgl 于 2009-12-18 13:01 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-12-17 09:06 |只看该作者
split函数是否无法把||作为分割符?

可以  split '\|\|',$str;

论坛徽章:
78
双子座
日期:2013-10-15 08:50:09天秤座
日期:2013-10-16 18:02:08白羊座
日期:2013-10-18 13:35:33天蝎座
日期:2013-10-18 13:37:06狮子座
日期:2013-10-18 13:40:31双子座
日期:2013-10-22 13:58:42戌狗
日期:2013-10-22 18:50:04CU十二周年纪念徽章
日期:2013-10-24 15:41:34巨蟹座
日期:2013-10-24 17:14:56处女座
日期:2013-10-24 17:15:30双子座
日期:2013-10-25 13:49:39午马
日期:2013-10-28 15:02:15
4 [报告]
发表于 2009-12-17 09:10 |只看该作者
关注下,切割文件没用过

论坛徽章:
0
5 [报告]
发表于 2009-12-17 09:21 |只看该作者
原帖由 guap514 于 2009-12-17 09:06 发表
split函数是否无法把||作为分割符?

可以  split '\|\|',$str;



呵呵,这样真的可以,我之前只是试了:
split /\|\|/,$str;是不可以的。

论坛徽章:
0
6 [报告]
发表于 2009-12-17 09:24 |只看该作者
#!/usr/bin/perl


use strict;
use warnings;

use IO::File;

my %FH;

$FH{'A'} = IO::File->new('> A.txt') or die;
$FH{'B'} = IO::File->new('> B.txt') or die;
$FH{'C'} = IO::File->new('> C.txt') or die;


while (<DATA>) {
&nbsp;&nbsp;&nbsp;&nbsp;chomp;
&nbsp;&nbsp;&nbsp;&nbsp;$FH{(split //)[0]}->print("$_\n");
}

__DATA__
A||87767
A||98786
A||F9878
B||8789
B||90887
C||8U798

论坛徽章:
0
7 [报告]
发表于 2009-12-17 09:31 |只看该作者
当然,如果你从外面读的话,把后面的代码修改一下


my $file = 'Your_file_name';
my $fh = IO::File->new($file) or die;

while ($_ = $fh->getline) {
        chomp;
        $FH{(split //)[0]}->print("$_\n");
}

论坛徽章:
0
8 [报告]
发表于 2009-12-17 09:44 |只看该作者
原帖由 cobrawgl 于 2009-12-17 09:24 发表
#!/usr/bin/perl


use strict;
use warnings;

use IO::File;

my %FH;

$FH{'A'} = IO::File->new('> A.txt') or die;
$FH{'B'} = IO::File->new('> B.txt') or die;
$FH{'C'} = IO::File->new(' ...



但是要创建多少个文件句柄是未知的哦,需要根据数据文件有多少个值来调整创建多少个文件句柄。

论坛徽章:
0
9 [报告]
发表于 2009-12-17 09:55 |只看该作者
写了一个繁琐的。

#!/usr/bin/perl
use warnings;
use strict;

my %hash;
open FHRead, "<", $ARGV[0] or die $!;
while(<FHRead>)
{
        chomp;
        my ($key, $value) = split /\|\|/, $_;
        unless($hash{$key})
        {
                $hash{$key} = [];
        }
        push @{$hash{$key}}, $value;
}

for my $key(keys %hash)
{
        my $result = $key."\.txt";
        open FHWrite, ">", $result or die $!;
        my @arr = @{$hash{$key}};
        for my $member(@arr)
        {
                print FHWrite $key."||$member\n";
        }
        close FHWrite;
}
close FHRead;

论坛徽章:
0
10 [报告]
发表于 2009-12-17 10:22 |只看该作者
原帖由 cobrawgl 于 2009-12-17 10:08 发表
#!/usr/bin/perl



use strict;
use warnings;

use IO::File;

my %FH;

while () {
    my $name = (split //)[0];
    $FH{$name} = IO::File->new("> $name.txt") unless exists $FH{$name}; ...



好的,谢谢!!万分感谢!
不过,还有一个问题是,打开的文件句柄如何关闭呢?

[ 本帖最后由 fikong2005 于 2009-12-17 10:34 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP