|
|
#1 |
|
Prospect
Join Date: Apr 2006
Posts: 1
|
Perl - Extracting question
Hello Everyone,
I'm not being familiar with Perl so I wondered if someone could point out how I can translate this to Perl': # myFile is a 200Mo tab separated value file. # e.g data in column 5: Client "JohnDoe" opening a connection from "192.168.0.20" Administrator connected: "PTMVSB9Z" (10.0.0.1). --------- perl -i for each line in "myFile" add a tab at the end of the line (new empty field) if column 5 begins with "Client" and contains pattern "opening" or pattern "closing" then grab the text between the first and second quote in column 5 add the result at the end of the line fi add a tab at the end of the line (new empty field) if column 5 begins with "Admin" or contains pattern "opening a connection" then grab the IP address in column 5 add the result at the end of the line ...etcfi done Any help would be greatly appreciated! Thanks Last edited by clem9756; 03-05-2007 at 08:08 AM. |
|
|
|
|
|
#2 |
|
Prospect
Join Date: Mar 2007
Posts: 19
|
Here's some code that should work (untested). Remember that perl's indices are 0-based, for [4] is the fifth column. My regular expressions use /x so they can be better documented, and I use variables to hold "interesting" patterns.
Code:
# /usr/bin/perl -w -i.bak
$OPENING_PAT = 'open';
$CLOSING_PAT = 'close';
$ADMIN_PAT = '(Admin|opening a connection)';
$INET_ADDR = '\b(\d{1,3}:\d{1,3}:\d{1,3}:\d{1,3})\b';
while (<>) {
tr /\r\n//d; # better than "chop/chomp"
@split = split("\t");
if ($split[4] =~ /^Client # match Client at start
.* # anything else (don't care)
${OPENING_PAT} # opening pattern
(.*) # stuff in between
${CLOSING_PAT} # closing pattern
/x) {
push @split, $1; # add new column
} elsif ($split[4] =~ /^${ADMIN_PAT} # match Admin/opening... at start
.* # anything else (don't care)
${INET_ADDR} # Internet Address
/x) {
push @split, $2;
}
print join("\t", @split) . "\n";
}
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|