我有一個TCPDUMP文件,其中包含許多單詞USER和PASS的用法,我需要制定一個正則表達式來查找它們,然後打印每個文件的數量。 (或任何不同的方式;正則表達式是我對這些問題的第一選擇)。我認爲我的分裂似乎並不正確。不知道我在這裏做錯了,所以有什麼想法?提前致謝!使用正則表達式找到一個匹配 - perl
下面是輸入文件的例子(注意:這僅僅是2006行的文件的第一行的格式是相同的,但數字,符號,和字母DO中的每一行改變)
22:28:28.374595 IP 98.114.205.102.1821 > 192.150.11.111.445: Flags [S], seq 147554406, win 64240, options [mss 1460,nop,nop,sackOK], length 0E...<[email protected] ...\.bfP....Y..echo open 0.0.0.0 8884 > USER 1 1 >>
代碼:
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
#opens txt file: read mode
open MYFILE, '<', 'source_file.txt' or die $!;
#opens output txt file: write mode
open OUT, '>', 'Summary_Report.txt' or die $!;
#open output txt file: write mode
#used to store header 'split' info
open OUTFILE, '>', 'Header.txt' or die $!;
my $start_time = undef;
my $end_time;
my $linenum = 0;
my $user;
my $pass;
while (<MYFILE>) {
chomp;
$linenum++;
#print ": $_\n"; ###if I need to see the lines (check)###
#separate pieces of information from TCPDUMP into list
my @header = split (' ',$_);
print OUTFILE "$linenum: @header\n\n";
if (/^22:28/ && !defined($start_time)) {
$start_time = $header[0];
#print "$start_time\n"; ###used as a check###
}
if ($_ = /22:28/) {
$end_time = $header[0];
}
if ($_ =~ m/USER/i) {
$user = $header[10];
}
}
print OUT "Total # of times phrases were used:\n\n
USER (variations thereof) = $user\n\n
PASS (variations thereof) = $pass\n\n\n";
請發佈您的輸入外觀。 – hwnd
希望我所做的幫助呀。 – user2288
我剛剛意識到我需要一些元字符,這些可能會有所幫助!不太確定如何去解決它們。 – user2288