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

Hi!

I want to print all the data between for example "42.95 and cache."

I have tried the two my ($vals) below.

my ($vals) = $_ =~ /\b\s\d+\.\d+\b|\b\d+\.\d+\b(.*?)\bcache\b/; my ($vals) = $_ =~ /\b\s|\d+\.\d+\b(.*?)\bcache\b/; __DATA__ 42.95 7712 15:20:05 08:30 23684 cache -s/usr/cachesys/mgr -cj -p +374 JOB^CCJOB or 2.18 7712 03:31:27 11:57:08 27747 cache -s/usr/cachesys/mgr -cj -p +91 JOB^CCJOB __OUTPUT__ perl test.plx Use of uninitialized value in print at test.plx line 34, <$ps> line 52 +7. Use of uninitialized value in print at test.plx line 34, <$ps> line 62 +9. Use of uninitialized value in print at test.plx line 34, <$ps> line 63 +1. Use of uninitialized value in print at test.plx line 34, <$ps> line 63 +4. Use of uninitialized value in print at test.plx line 34, <$ps> line 63 +5. 7712 00:47:40 14:44:41 12051 7712 00:47:42 14:44:39 12052 7712 00:47:44 14:44:37 12055 7712 00:47:49 14:44:32 12090 . . . .

Replies are listed 'Best First'.
Re: print data between with regexp...again
by kennethk (Abbot) on Nov 02, 2010 at 19:46 UTC
    The warnings are being issued due to code you are not showing us. When crafting posts, please create small, complete programs that replicate your issue; see How do I post a question effectively?.

    The code

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { print /\b\s\d+\.\d+\b|\b\d+\.\d+\b(.*?)\bcache\b/; print "\n"; print /\b\s|\d+\.\d+\b(.*?)\bcache\b/; print "\n"; } __DATA__ 42.95 7712 15:20:05 08:30 23684 cache -s/usr/cachesys/mgr -cj -p +374 JOB^CCJOB 42.18 7712 03:31:27 11:57:08 27747 cache -s/usr/cachesys/mgr -cj -p +91 JOB^CCJOB

    outputs

    7712 15:20:05 08:30 23684 7712 15:20:05 08:30 23684 7712 03:31:27 11:57:08 27747 7712 03:31:27 11:57:08 27747

    where I have not modified your regular expressions.

      understood: here is my code.
      open (my $ps, "-|", "/usr/bin/ps -o pcpu,vsz,stime,etime,pid,args -A|s +ort -kn1") or die "ps not opened $!"; while (<$ps>) { next if $. == 1; next if /\/usr\/cachesys\/mgr -U/i; ### Skip interactive sessions +### #my @tmp = split if /cache/ig; if ( /(cache\s-s.*|\/usr\/cachesys.*)/ig ) { #print $1,"\n"; my @tmp = split; my ($key) = split " ",$tmp[0]; #my ($vals)= $_ =~ /([^cache]*)/; my ($vals) = $_ =~ /\b\s|\d+\.\d+\b(.*?)\bcache\b/; print $vals,"\n"; } #my @tmp1=@tmp[1,2,3,4]; #$psH{$key} = [@tmp1]; } close ($ps) __SAMPLE DATA__ 8.45 7712 15:50:37 01:18 27551 cache -s/usr/cachesys/mgr -cj -p2 +95 JOB^CCJOB 9.61 7712 15:50:39 01:16 27661 cache -s/usr/cachesys/mgr -cj -p +295 JOB^CCJOB 9.69 7712 15:50:35 01:20 27501 cache -s/usr/cachesys/mgr -cj -p +295 JOB^CCJOB 9.87 7712 15:50:41 01:14 27739 cache -s/usr/cachesys/mgr -cj -p +295 JOB^CCJOB 10.62 7712 15:50:43 01:12 27815 cache -s/usr/cachesys/mgr -cj -p +295 JOB^CCJOB 23.68 7712 10:51:28 05:00:27 15822 cache -s/usr/cachesys/mgr -cj -p +305 JOB^CCJOB
        Still can't replicate. I changed your system call to ps -o pcpu,vsz,stime,etime,pid,args -A|sort -nk1 (note the swapped arg list on sort) and had no trouble, but if the problem were your system call you'd have different behaviors. I assume line 34 corresponds to print $vals,"\n";. Do you see expected values if you comment out your lines 33 and 34 and insert print; (which will print the content of the special variable $_)? If I modify your given sample data to run in a DATA block and again use your loop unmodified, I get the output

        7712 15:50:39 01:16 27661 7712 15:50:35 01:20 27501 7712 15:50:41 01:14 27739 7712 15:50:43 01:12 27815 7712 10:51:28 05:00:27 15822