将perl脚本文本修改为csv

时间:2011-04-01 21:33:24

标签: perl csv

您好我写了一个perl脚本来输出带有端口扫描的文本文件excel现在我需要格式化文本文件,以便当它打印到excel时它是csv格式。比如这样 服务器,端口,协议,状态 69.25.194.14,25,tcp,http

这是我的代码,我希望你们可以修改,代码到目前为止输出txt文件到excel这是好的现在我只需要它修改,以便它可以在文本文件中以csv格式显示它并输出txt文件要出类拔萃 :

$input = `Cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'SYN Stealth' |grep -v'Discovered'`;
chomp input;
$output =" /cygdrive/c/Users/bpaul/Desktop/194.csv ";
if (! -e "$output")
{
`touch $output`;
}
open (OUTPUTFILE, ">$output") || die "Can't Open file $output";
print OUTPUTFILE "$input\n";
close (OUTPUTFILE);

这是我的txt文件

Nmap scan report for 69.25.194.2 Host is up (0.072s latency). 
Not shown: 9992 filtered ports PORT STATE SERVICE 
25/tcp open smtp
80/tcp open http
82/tcp open xfer
443/tcp open
https 4443/tcp closed
pharos 5666/tcp closed
nrpe 8080/tcp closed
http-proxy 9443/tcp closed tungsten-https

请有人修改我的代码吗?谢谢!

2 个答案:

答案 0 :(得分:0)

一旦你在什么列中找到你想要的东西(我无法从你的问题中得知),我会看Text::CSV::print输出。

答案 1 :(得分:0)

这很接近,但输入需要更多过滤。

#!/usr/bin/env perl

use strict;
use warnings;

use Text::CSV;
my $csv = Text::CSV->new();

my $in_file = '/cygdrive/c/Windows/System32/test11.txt';
open my $in_fh, '<', $in_file or die "could not open $in_file: $!\n";

my @rows = ();
while( my $line = <$in_fh> ){
  chomp $line;
  next if $line =~ m{ SYN Stealth }msx;
  next if $line =~ m{ Discovered }msx;

  push @rows, [ split m{ \s+ }msx, $line ];
}
close $in_fh or die "could not close $in_file: $!\n";

$csv->eol( "\n" );

my $out_file = '/cygdrive/c/Users/bpaul/Desktop/194.csv';
open my $out_fh, '>', $out_file or die "could not open $out_file: $!\n";

for my $row ( @rows ){
  $csv->print( $out_fh, $row ) or die "could not print to $out_file: $!\n";;
}
close $out_fh or die "could not close $out_file: $!\n";
相关问题