LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-24-2011, 09:52 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
grab last few lines until meet the first pattern


Dear Experts,

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.

Last edited by cristalp; 10-24-2011 at 09:53 AM.
 
Old 10-24-2011, 10:06 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
How about:

Code:
sed '1,/pattern/d' file > newfile
Would it be what you're after?

Last edited by sycamorex; 10-24-2011 at 10:08 AM.
 
Old 10-24-2011, 10:19 AM   #3
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sycamorex View Post
How about:

Code:
sed '1,/pattern/d' file > newfile
Would it be what you're after?
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".
 
Old 10-24-2011, 10:25 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by cristalp View Post
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.
 
Old 10-24-2011, 10:33 AM   #5
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sycamorex View Post
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.
Thanks a lot! Take your time, don't need to be so hurry. I will also try something more and let you know if I get some results.
 
Old 10-24-2011, 10:58 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well a not so clean way:
Code:
tac file | sed '/PATTERN/,$ d' | tac
 
Old 10-24-2011, 11:25 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
You could also try:
Code:
sed -n '/PATTERN/! {H;${g;s/\n//;p};b};z;h' file
 
Old 10-24-2011, 01:10 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
hmmm, indeed. My solution doesn't work. I thought you could mix line numbers and patterns.
 
Old 10-24-2011, 05:18 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
@sycamorex

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.

http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

And the ever-popular grymoire tutorial:

http://www.grymoire.com/Unix/Sed.html

Last edited by David the H.; 10-24-2011 at 05:26 PM.
 
Old 10-24-2011, 06:13 PM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by David the H. View Post
@sycamorex

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.

http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

And the ever-popular grymoire tutorial:

http://www.grymoire.com/Unix/Sed.html
Thanks David. I know the links. Mind you I haven't visited them in months.

Initially, I thought that sed, being greedy as always, will match as many instances of PATTERN as possible.

My bad.
 
Old 10-24-2011, 06:22 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 10-24-2011, 09:58 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe a little awk:
Code:
awk '{l = l (l?"\n":"") $0}/PATTERN/{l=""}END{print l}' file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Find pattern that extends over several lines porphyry5 Programming 3 06-12-2011 11:08 AM
Find pattern and comment out 2 lines after it ariszlo Linux - Newbie 3 10-14-2010 05:37 AM
How to delete/grab a line which matchs a pattern of a particular column only ? mauludi Linux - Newbie 6 01-18-2010 05:52 PM
grep lines between occurances of a pattern ksri07091983 Solaris / OpenSolaris 5 04-26-2009 06:14 AM
Replacing a bunch of lines between a pattern Namachivayam Programming 1 05-21-2007 07:23 PM

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

All times are GMT -5. The time now is 09:01 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