【问题标题】:Modify perl script text to csv将 perl 脚本文本修改为 csv
【发布时间】:2011-04-01 21:33:24
【问题描述】:

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

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

$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

谁能修改我的代码。谢谢!

【问题讨论】:

标签: perl csv


【解决方案1】:

一旦您在哪些列中确定了您想要的内容(我无法从您的问题中看出),我会查看 Text::CSV::print 的输出。

【讨论】:

    【解决方案2】:

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

    #!/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";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 2013-07-21
      • 2014-05-03
      • 2017-03-11
      • 2016-11-10
      • 2019-06-23
      相关资源
      最近更新 更多