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 want to read a file and save last few lines into a new file.
The number of the lines I need was defined by the first "PATTERN" counted from the bottom line of the file. For example, suppose my file looks like:
Code:
First line of the file
line 2
line 3
...
...
line 98
"PATTERN"
line 100
line 101
line 102
...
...
line 187
"PATTERN"
line 189
line 190
...
...
line 308
"PATTERN"
line 310
line 311
...
...
line 359
line 360
bottom line(the last line of the file)
What I want to grab from the file and save to the new file is:
Code:
line 310
line 311
...
...
line 359
line 360
bottom line(the last line of the file)
I do NOT know in advanced where the "PATTERN" locate and how many lines between each of them.
How could I do this easily in bash script?
I would appreciate any help! Thanks for your time.
Thanks, I tried your code, but it doesn't work for my purpose. It print out a lot more lines which I do not need. I only need the lines bewteen the last line and the last "PATTERN".
Thanks, I tried your code, but it doesn't work for my purpose. It print out a lot more lines which I do not need. I only need the lines bewteen the last line and the last "PATTERN".
I'm not at my linux computer right now. Once I get home I'll double check it. In the meantime, someone else will probably correct it.
sed does allow you to mix&match. It's just that your command matches from line 1 to the first instance of the pattern in the file, and he needs it to match to the last instance.
Notice how grail's solution works, by reversing the file first.
Edit: While they don't appear to address this problem specifically, these links can may you to work out such complex situations.
sed does allow you to mix&match. It's just that your command matches from line 1 to the first instance of the pattern in the file, and he needs it to match to the last instance.
Notice how grail's solution works, by reversing the file first.
Edit: While they don't appear to address this problem specifically, these links can may you to work out such complex situations.
I actually posted the links mostly for the OP, but I guess I didn't make that clear.
And I wouldn't call sed itself greedy, just the regular expressions that you often use in it. When it comes to multi-line operations, sed can often be a real pain to use.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.