LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2008, 12:12 PM   #1
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
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
 
Old 07-08-2008, 12:18 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Does it have to be perl ?

tail - 3 file | head -2
 
Old 07-08-2008, 12:30 PM   #3
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by Mr. C. View Post
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.
 
Old 07-08-2008, 04:31 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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

Last edited by Mr. C.; 07-08-2008 at 08:16 PM.
 
Old 07-08-2008, 07:53 PM   #5
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by Mr. C. View Post
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.
 
Old 07-08-2008, 08:15 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Oops on the @lines[-1]. Just checking to see if you were paying attention!

Cheers
 
Old 07-08-2008, 08:53 PM   #7
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by Mr. C. View Post
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.
 
Old 07-08-2008, 08:57 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
Old 07-08-2008, 09:30 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - how to capture output of a command with more than one args new_2_unix Linux - Newbie 2 11-29-2007 10:55 PM
Perl regex $ ShaqDiesel Programming 6 08-18-2006 02:40 PM
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
perl - regex - negation beebop Programming 9 06-19-2005 08:25 PM
HELP ->PERL regex is kick'n my a... prisoner Programming 2 03-18-2004 09:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration