简体   繁体   English

perl脚本仅向perl中的输出文件写入一行

[英]perl script only write one row to output file in perl

i wrote a script to open a file on web, and pull out all rows with wireless in the name. 我写了一个脚本来打开Web上的文件,并以无线名称的形式拉出所有行。 It writes the out put to a different file, but it only records one line in the output file, should be mulitipe lines. 它把输出写到另一个文件中,但它只在输出文件中记录一行,应该是多行。

#!\Perl64\eg\perl -w
use warnings;
use strict;

use LWP::Simple;

my $save = "C:\\wireless\\";
my $file = get 'http://dhcp_server.test.com/cgi-bin/dhcp_utilization_csv_region.pl?region=test';

open( FILE, '>', $save . 'DHCP_Utilization_test.csv' ) or die $!;
binmode FILE;
print FILE $file;
close(FILE);

open( F, "C:\\wireless\\DHCP_Utilization_test.csv" ) || die "can't opern file: $!";
my @file = <F>;
close(F);

my $line;

foreach $line (@file) {
    chomp $line;
    if ( $line =~ m/Wireless /g ) {

        my ($ip,     $rtr,   $mask,    $zip,    $blc, $address, $city,
            $state,  $space, $country, $space2, $noc, $company, $extra,
            $active, $used,  $percent, $extra3, $nus, $construct
        ) = split( /,/, $line );

        my $custom_directory = "C:\\wireless\\";
        my $custom_filename  = "wireless_DHCP.csv";
        my $data             = "$ip $mask $rtr $active $used $percent $nus $construct";

        my $path = "$custom_directory\\$custom_filename";

        open( my $handle, ">>", $path ) || die "can't open $path: $!";
        binmode($handle);    # for raw; else set the encoding

        print $handle "$data\n";

        close($handle) || die "can't close $path: $!";
    }
}

I believe the problem is because you're on Windows, but then saving the file using :raw , and then reopening it using :crlf . 我相信问题是因为您在Windows上,但随后使用:raw保存文件,然后使用:crlf重新打开文件。

open( FILE, '>', $save . 'DHCP_Utilization_test.csv' ) or die $!;
binmode FILE;
print FILE $file;
close(FILE);

open( F, "C:\\wireless\\DHCP_Utilization_test.csv" ) || die "can't opern file: $!";
my @file = <F>;
close(F);

I therefore suspect that your @file array only contains one line for the entire file. 因此,我怀疑您的@file数组仅包含整个文件的一行。

You can probably also tighten your code to something like the following: 您可能还可以将代码收紧为以下内容:

#!\Perl64\eg\perl
use strict;
use warnings;
use autodie;

use LWP::Simple;

my $url = 'http://dhcp_server.test.com/cgi-bin/dhcp_utilization_csv_region.pl?region=test';

my $datafile = "C:\\wireless\\DHCP_Utilization_test.csv";
my $wireless = "C:\\wireless\\wireless_DHCP.csv";

getstore( $url, $datafile );

open my $infh,  '<',  $datafile;
open my $outfh, '>>', $wireless;

while (<$infh>) {
    chomp;
    next unless /Wireless /;

    my ($ip,     $rtr,   $mask,    $zip,    $blc, $address, $city,
        $state,  $space, $country, $space2, $noc, $company, $extra,
        $active, $used,  $percent, $extra3, $nus, $construct
    ) = split /,/;

    print $outfh "$ip $mask $rtr $active $used $percent $nus $construct\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM