Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Splitting Across Multiple Lines

by krazken (Scribe)
on Mar 13, 2002 at 19:52 UTC ( [id://151503]=note: print w/replies, xml ) Need Help??


in reply to Splitting Across Multiple Lines

I would look at it from a different perspective...By putting them into arrays, you lose what is associated with what, so I would look at anonymous hashes to do the trick.
#!/usr/bin/perl -w use strict; my $ip=''; my %hash=(); while(<>) { chomp; if (/\+/) { #we have an ip address...so parse it. my $temp=$_; $temp=~s/^\+\s//g; $ip=(split (/\s/,$temp))[0]; $ip=~s/\s//g; } if ($ip !~ /\d{3}\.\d{3}\.\d{1,3}\.\d{1,3}/) { #this should never happen print STDERR "Don't have an IP address...something is not righ +t here...\n"; exit (-1); } push(@{$hash{$ip}},$_); } ###########################################3 #this will print a table like #IPADDRESS=<the ip address of the server> # SERVER=<server name> # PORTNUMBER=<port number> # SERVICE=<what service is running> # DESCRIPTION=<description of service> ####################################################### foreach my $ip_address (keys %hash) { print "IPADDRESS=$ip_address\n"; my $loop_num=0; foreach my $information (@{$hash{$ip_address}}) { #here is where you parse the information... #to get the servername, etc. #print "$information\n"; if ($loop_num == 0) { #you know this is the first entry, so this will have the s +erver name my $server=(split(/\s/,$information))[-1]; print "\t\tSERVER=$server\n"; } elsif ($loop_num == 1) { #this will have the port number if ($information =~ /(\d+)/) { my $port_number=$1; print "\t\tPORTNUMBER=$port_number\n"; } my $service=(split(/\|\_\_\_/,$information))[1]; $service=join(" ",split(" ",$service)); #need to get the port number off the front... $service=~ s/^\d+//g; $service=join(" ",split(" ",$service)); print "\t\tSERVICE=$service\n"; } elsif ($loop_num == 2) { #here is your description my $description=(split(/\|\_\_\_/,$information))[1]; $description=join(" ",split(" ",$description)); print "\t\tDESCRIPTION=$description\n"; } $loop_num++; } }
I took the * off of the example you gave as well as fixed the word wrap that happened, and it gave the following results...
IPADDRESS=192.168.29.21 SERVER=orcweb.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220 sandbox FTP server (Version 1.1.214.7 Thu Aug +10 09:57:38 GMT 2000) ready... IPADDRESS=192.168.31.3 SERVER=ftp.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220 ftp.acme.com FTP server ready... IPADDRESS=192.168.33.19 SERVER=acmeftp.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220-acme Secure FTP Server.. WarFTPd 1.71.02 (Feb +14 2000) Ready.. (C)opyright 1996 - 2000 by Jarle (jgaa) Aase - all r +igh IPADDRESS=192.168.89.1 SERVER=acmeorp.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] IPADDRESS=192.168.11.22 SERVER=commerce.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220 commerce Microsoft FTP Service (Version 4.0).. +. IPADDRESS=192.168.19.24 SERVER=webapp.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220 WEBAPP1PRI Microsoft FTP Service (Version 5.0) +...
Output is not in the same order as the input, but you can fix that by sorting the hash keys. Anonymous hashes are my method of choice whenever I have to group lines/records by a unique key. I know this is different than you were asking, but doing it in this fashion makes it a little more flexible to work with since you know the order that the data will come in. cheers! krazken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://151503]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-05-27 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found