LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to capture this output with perl regex (https://www.linuxquestions.org/questions/programming-9/how-to-capture-this-output-with-perl-regex-654346/)

WindowBreaker 07-08-2008 12:12 PM

How to capture this output with perl regex
 
I have some output in a log file that looks like this:

Quote:

--------------------------------------
output
output
--------------------------------------
output
output
--------------------------------------
bla
bla
--------------------------------------
I would like to capture what's in between the last two lines in the output. Using the example above, I'd like to capture the "bla" lines into a scalar. I don't care if the dashed lines are included or not. I just need to be sure that I have all lines between the last two dashed lines, and nothing before it.

Can anyone show me how to do this?

TIA,

Pablo

Mr. C. 07-08-2008 12:18 PM

Does it have to be perl ?

tail - 3 file | head -2

WindowBreaker 07-08-2008 12:30 PM

Quote:

Originally Posted by Mr. C. (Post 3207804)
Does it have to be perl ?

tail - 3 file | head -2

It does, because this is part of a larger perl script. Also, there is an arbitrary number of lines between the dashed lines, not just two. Some can have 5 lines, some 20.

Mr. C. 07-08-2008 04:31 PM

Here's an easy way for you to follow. Since I helped, maybe you could follow-up here and explain how it works so that others can learn too.

perl -e 'undef $/ ; $_ = <> ; @lines = split /-+\n/ ;print $lines[-1]' datafile

WindowBreaker 07-08-2008 07:53 PM

Quote:

Originally Posted by Mr. C. (Post 3208059)
Here's an easy way for you to follow. Since I helped, maybe you could follow-up here and explain how it works so that others can learn too.

perl -e 'undef $/ ; $_ = <> ; @lines = split /-+\n/ ;print @lines[-1]' datafile

Thanks for the response. I haven't tested it yet, but it seems like it will work. I will test it tonight when I'm back at my office.

I will try to explain what I understand, but I'm no perl guru.

Code:

perl -e
says to run perl, and execute the following lines of code

Code:

undef $/
This enables 'slurp mode' to read the entire file into a variable, without regard to newlines.

Code:

$_ = <>
Reads the file named 'datafile' passed on the command line, into the variable named $_

Code:

@lines = split /-+\n/
Creates an array of elements. Each element contains the group of lines between two sets of dashed lines. This was done by the 'split' function, which splits a scalar on the regex /-+\n/ and stores everthing into an element of @lines.

Code:

print @lines[-1]
This prints the last element of the @lines array, by using a negative index. *This should read $lines[-1] since we're reading a single element of an array, which is a scalar.

Mr. C. 07-08-2008 08:15 PM

Oops on the @lines[-1]. Just checking to see if you were paying attention! ;)

Cheers

WindowBreaker 07-08-2008 08:53 PM

Quote:

Originally Posted by Mr. C. (Post 3208229)
Oops on the @lines[-1]. Just checking to see if you were paying attention! ;)

Cheers

Actually, I just tested it and we're both right. The following 2 lines of code produce the same output (2).
perl -e '@a = (1, 2); print @a[-1];'
perl -e '@a = (1, 2); print $a[-1];'

Also, I tried your suggestion and it worked like a charm - thanks. Slurp then split, that's great. The more I learn about perl the more I like it.

Thanks again.

Mr. C. 07-08-2008 08:57 PM

I tested that it worked, and that's what caused me not to notice my command-line edit typo.

You certainly don't want to be slurping huge files this way, but I supposed your demands weren't that great.

Perl is pretty fun. A guy in the Ubuntu forums asked how he could perform some automagic optimization on his assembler code. Some of the responses came back with ungodly solutions. I gave him this:

Code:

#!/usr/bin/perl -i.orig

undef $/;
$_ = <>;

s/\tmovl\t(\$[^,]*), +%eax\n\tmovl\t%eax, +(.*)/\tmovl\t$1, $2/g ;

print "$_";

He was thrilled, others were aghast.

chrism01 07-08-2008 09:30 PM

Re the array v scalar;
yeah, not recommended to use array where you mean scalar, but Perl has a lot of DWIM (Do What I Mean) :)


All times are GMT -5. The time now is 05:54 AM.