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.
From a text file like below, I want to extract the last paragraph (note paragraphs are separated by
“----------------------------------------------------------“ and not by blank lines)
So in example below I would like to extract only the paragraph in red
Thanks, unfortunately the number of lines in each paragraph is unknown and always varies and therefore need a way to extract the last paragraph based on the paragraph seperator (-----------------)
These arguments are ed commands. First one $k makes ed to go to the last line in the file. Second one uses reverse search to find delimiter and then prints lines to end of file (skipping delimiters, note +1 and -1).
If I read it right:
"read a new line, and then keep appending lines to the working register until we find 5 or more "-", then, if we are at the end of the file, print the accumulated record. Otherwise go back to the default collection of lines (without appending)."
this of course only works if there is a line of ------- at the end of the last record.
Last edited by pixellany; 07-16-2012 at 04:19 AM.
Reason: Fixed error---see later posts
If I read it right:
"read a new line, and then keep appending lines to the working register until we find 5 or more "-", then, if we are at the end of the line, print the accumulated record. Otherwise go back the default collection of lines (without appending)."
this of course only works if there is a line of ------- at the end of the last record.
That's right, except for this part
Quote:
if we are at the end of the line
which should read
Quote:
if we are at the end of the file
because $ in the address position means last line of the file.
Somewhat ugly solution to the last problem you mentioned is as follows:
Code:
$ sed -rn '{:a; /-{5,}/be; $be; N; ba}; :e; $p' in
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.