Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks, I have the following text:
"(PDH-TSV 4.0) (Eastern Daylight Time"(240)" "\\PLAGUE\Memory\Pages/sec" "\\PLAGUE\Network Interface(3Com EtherLink PCI)\Bytes Total/sec" "\\PLAGUE\PhysicalDisk(_Total)\Disk Transfers/sec" "\\PLAGUE\Processor(_Total)\% Processor Time"
It is a string of 5 cloumn names, and I want to shorten them to the following:
Date/Time Memory\Pages/sec Bytes Total/sec Disk Transfers/sec % Processor Time
My idea was to split them on \t push them onto an array, and format each element with the following code:
#!C:\Perl\bin\Perl -w open (LOG, "$Perf_rev") || die "Error: Couldn't open $Perf_rev : $!\n"; # don't include Header (No numerical data) my $header = <LOG>; @header = split(/\t/, $header); $header[1] =~ s/^\".*\"$/Date\/Time/; print $header[1];
I'm stuck on the remaining 4 elements of the array any suggestions? Thanks

Replies are listed 'Best First'.
Re: REGEXP Help
by arunhorne (Pilgrim) on Jun 24, 2002 at 13:24 UTC

    First up... if you always have the same lines why don't you just hard code the transformation, i.e $header[0] = "Date/Time"?

    Another note is that where you say $header[1] =~ s/^\".*\"$/Date\/Time/; in your script I think you mean $header[0] =~ s/^\".*\"$/Date\/Time/; as array indexing begins at zero...

    These points aside you are going to need a regular expression that can cope with the machine name syntax... how about:

    s/^\\\\(.*)\\.*$/your_text_here/;

    Note that its probably possible to write the above in a neater way due to the escaping of multiple backslahses. The brackets around the first .* in the expression act as a memory allowing you to retrieve the exact text they macthed, in this case the machine name... thus if you wanted to include the machine name in the text that replaces the macthed text you could use \1 or $1. Check out a regex text for information on these.

    Hope this helps... your questions a bit vague so clarify it a bit more and i'll try and help some more.

    ____________
    Arun
Re: REGEXP Help
by bronto (Priest) on Jun 24, 2002 at 13:39 UTC

    For all but the first line, I think that you could try the substitution s/^.*\\//. BTW, comments by arunhorne hold for me, too.

    --bronto

    update: uhm... are the quotes part of the strings or not?

    update 2: uhm... someone is trying to make me understand that the quotes are really there... That's ok.

    how about this?

    # first of all, adjust array subscripts, as suggested by arunhorne; # then set $header[0] ; # then... while (@header < 5) { $header = <LOG> ; $header =~ /^".*\\(.*)"$/ and push @header, $1 ; }

    ...and that should do the job.

Re: REGEXP Help
by particle (Vicar) on Jun 24, 2002 at 13:37 UTC
    #!/usr/bin/perl use strict; use warnings; $|++; while( <DATA> ) { next unless /^"\\\\/; # valid lines only chomp; # remove EOL my @column_pieces = split /\\/, $_, 5; # split on \, max 5 fields print $column_pieces[-1]=~/^(.*)"$/,$/; # display last (fifth) field } __DATA__ "(PDH-TSV 4.0) (Eastern Daylight Time"(240)" "\\PLAGUE\Memory\Pages/sec" "\\PLAGUE\Network Interface(3Com EtherLink PCI)\Bytes Total/sec" "\\PLAGUE\PhysicalDisk(_Total)\Disk Transfers/sec" "\\PLAGUE\Processor(_Total)\% Processor Time"
    prints:

    Pages/sec Bytes Total/sec Disk Transfers/sec % Processor Time

    ~Particle *accelerates*