- 论坛徽章:
- 0
|
请问如何让文件分为单数行与双数行分别输出!
原帖由 "perl911" 发表:
俺想把下面的文件按单行与双行分别输出:
Tennesse
Tenney
Tennis
Tenochti
输出:
文件1(单数行)
Tennesse
Tennis
文件2(双数行)
Tenney
Tenochti
俺是初学者,请大家帮忙,学习一下!谢谢..........
- #!/usr/bin/perl -w
- use strict;
- my $fileSource="10.tmp";
- my $fileOut1="file1.txt";
- my $fileOut2="file2.txt";
- open (FILE0,$fileSource) or die "$!";
- open (FILE1,">;",$fileOut1) or die "$!";
- open (FILE2,">;",$fileOut2) or die "$!";
- my $i=0;
- while(<FILE0>;)
- {
- next if /^$/;
- unless ($i%2)
- {
- print FILE1 $_;
- }else
- {
- print FILE2 $_;
- }
- $i++;
- }
- close FILE2;
- close FILE1;
- close FILE0;
复制代码 |
|