将perl脚本文本修改为csv

发布于 2024-10-29 10:37:18 字数 944 浏览 1 评论 0原文

您好,我编写了一个 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

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

Hi I wrote a perl script to output a text file with port scans to excel now I need to format the text file so that when it prints to excel it's in csv format. Like this for example
Server, port, protocol, state
69.25.194.14, 25, tcp, http

Here is my code that I hope you guys could modify, The code so far outputs the txt file to excel which is good now I just need it modified so that it can display it in csv format within the text file and output the txt file to 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);

Here is a piece of my txt 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

Could anybody please modify my code. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2024-11-05 10:37:18

一旦你弄清楚你想要什么列(我无法从你的问题中看出),我会看看 Text::CSV::print 用于输出。

Once you figure what you want in what columns (I can't tell from your question), I'd look at Text::CSV::print for output.

花伊自在美 2024-11-05 10:37:18

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

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

This is close but the input needs more filtering.

#!/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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文