CSV文件格式 [英] CSV File Formatting

查看:194
本文介绍了CSV文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个perl脚本来输出一个文本文件,填充了ip地址和端口,我扫描到微软excel。现在的数据是在excel我的老板要我组织的csv格式的文件,如

 服务器,端口,协议,随机,随机,随机
ns1,25,tcp,stuff,stuff,stuff



 

!/ usr / bin / perl

$ input =`Cat /cygdrive/c/Windows/System32/test11.txt | grep -v'SYN Stealth'`
chomp input;
$ output =/cygdrive/c/Users/bpaul/Desktop/194.csv;
if(!-e$ output)
{
`touch $ output`
}
open(OUTPUTFILE,> $ output)|| die无法打开文件$ output;
print OUTPUTFILE$ input\\\
;
close(OUTPUTFILE);

这是我的一个文件

  69.25.194.2的Nmap扫描报告主机已启动(0.072s延迟)。未显示:9992过滤的端口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

到目前为止,我的代码采用了我的txt文件并输出到excel现在我需要格式化数据,如下:



 服务器端口协议随机随机
ns1,25,tcp,stuff,stuff, stuff


解决方案

我假设你的意思是<$ c $当您说 ns1 时, 69.25.194.2

  use strict; 
使用警告;

使用Text :: CSV_XS qw();

my $ csv = Text :: CSV_XS-> new({binary => 1,eol =>\\\
});

$ csv-> print(\ * STDOUT,[qw(
Server
port
protocol
random
random
random
)]);

my $ host ='[unknown]';

while(<>){
$ host = $ 1 if / Nmap scan report for(\S +)/

my($ port,$ protocol)= m {(\d +)/(\w +)(?:open | closed)/
或next;

$ csv-> print(\ * STDOUT,[
$ host,
$ port,
$ protocol,
'stuff'
'stuff',
'stuff',
]);
}

用法:

  grep -v'SYN Stealth'/cygdrive/c/Windows/System32/test11.txt | perl to_csv.pl> /cygdrive/c/Users/bpaul/Desktop/194.csv 

Text :: CSV_XS



更新:用已扫描机器的地址替换硬编码 ns1



将普通用法替换为OP将使用的。


I wrote a perl script to output a text file filled with ip addresses and ports that i scanned into microsoft excel. Now that the data is in excel my boss wants me to organize the file in csv format such as

Server, port, protocol, random, random, random
ns1,    25,   tcp,      stuff,  stuff,  stuff

Can any one help me with this Please?

Code:

#!/usr/bin/perl

$input = `Cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'SYN Stealth'`;
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);

Here is a piece of my file

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

So far my code took my txt file and outputted it to excel now I need to format the data like this:

Desired Output:

Server, port, protocol, random, random, random
ns1,    25,   tcp,      stuff,  stuff,  stuff

解决方案

I'm assuming you meant 69.25.194.2 when you said ns1.

use strict;
use warnings;

use Text::CSV_XS qw( );

my $csv = Text::CSV_XS->new({ binary => 1, eol => "\n" });

$csv->print(\*STDOUT, [qw(
    Server
    port
    protocol
    random
    random
    random
)]);

my $host = '[unknown]';

while (<>) {
    $host = $1 if /Nmap scan report for (\S+)/;

    my ($port, $protocol) = m{(\d+)/(\w+) (?:open|closed)/
        or next;

    $csv->print(\*STDOUT, [
        $host,
        $port,
        $protocol,
        'stuff',
        'stuff',
        'stuff',
    ]);
}

Usage:

grep -v 'SYN Stealth' /cygdrive/c/Windows/System32/test11.txt | perl to_csv.pl > /cygdrive/c/Users/bpaul/Desktop/194.csv

Text::CSV_XS

Update: Replaced hardcoded ns1 with address of scanned machine.

Update: Replaced generic usage with what the OP would use.

这篇关于CSV文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆