Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 03-05-2007, 04:47 AM   #1
clem9756
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
fi
...etc
done


Any help would be greatly appreciated!
Thanks

Last edited by clem9756; 03-05-2007 at 08:08 AM.
clem9756 is offline   Reply With Quote
Old 03-07-2007, 04:05 PM   #2
lar3ry
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";
}
lar3ry is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 10:12 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.