ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.