CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Split or Cut or traverse file with multi char delimiter

 



perlzz
New User

Mar 16, 2011, 5:58 AM

Post #1 of 2 (1199 views)
Split or Cut or traverse file with multi char delimiter Can't Post

Hi All
basically i want to know i can split on multi char delim's eg if my delimiter is cpu ...

Below is the detailed description of what I want to achieve, any other ways to do it are most welcome
-----------------
Below is an example of a file that my perl script gets as input

eg:Lets call this DATA File(just an example)
timeStamp server1 cpu=20% memory=10% param3=40 param4=50
timeStamp server2 cpu=2% memory=1% param3=401 param4=501
timeStamp server3 cpu=10% memory=20% param3=4 param4=502
timeStamp server4 cpu=22% memory=11% param3=0 param4=503

I have a paramFile which mentions list of params
eg: PARAM File(just an example, param list can keep on changing wrt environment)
paramList=cpu,param4

I want to make a generic script that reads the DATA File and makes a csv of the parameters mentioned in the paramList

Sorry for the big intro as to what I want to do .. but now where am I stuck...

-- I can read the param name from the paramList
-- but then to cut out/split the line that i read from the DATA File want to know how I can split using delimeter like "cpu" or "memory" etc
-- Basically multi char splitting/cut
(around 11K lines every day, so cutting with " " and then search for the paramName and then store the value makes things too slow)


Karazam
User

Mar 16, 2011, 6:42 AM

Post #2 of 2 (1194 views)
Re: [perlzz] Split or Cut or traverse file with multi char delimiter [In reply to] Can't Post

With Perl's split function you can split on a regular expression, so for example


Code
split /cpu/, $string;


I'm not sure that's what you want, though. Perhaps something like this will work for you:


Code
#!perl 
use warnings;
use strict;

my @paramList = qw(cpu param4);

while ( my $line = <DATA> ) {
chomp $line;
for my $i ( 0 .. $#paramList ) {
if ( $line =~ /$paramList[$i]=(.*?)(\s|$)/ ) {
print "$paramList[$i],$1";
print $i < $#paramList ? ',' : "\n";
}
}
}

__DATA__
timeStamp server1 cpu=20% memory=10% param3=40 param4=50
timeStamp server2 cpu=2% memory=1% param3=401 param4=501
timeStamp server3 cpu=10% memory=20% param3=4 param4=502
timeStamp server4 cpu=22% memory=11% param3=0 param4=503


Probably that's not exactly what you're after, but maybe it's a start. Hope this helps. Smile


(This post was edited by Karazam on Mar 16, 2011, 11:26 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives