LinuxQuestions.org
Help answer threads with 0 replies.
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 02-22-2011, 05:01 AM   #1
sylaan
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Rep: Reputation: 0
Extracting the last section of a file


Hi all,

I have a file which looks something like this:

##########

some
text

text also includes empty lines

##########

some
more
text

##########



Basically all sections are separated by 10 hashes and I need to somehow only print all lines in the last section (the "some more text" part in the example above").

I tried all kind of things with sed and awk but I didn't find any way to identify the last "section".

Any help is appreciated.

Thanks,
Sylaan
 
Old 02-22-2011, 06:54 AM   #2
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
This works for some versions of awk. Depends whether or not it supports multiple character record separators.
Code:
awk '{RS="##########";} END{print}'
 
Old 02-22-2011, 07:25 AM   #3
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
Actually, as the data ends with the separator the above will not work as the last entry is the empty data after the last separator.
Maybe try:
Code:
awk '/[^[:space:]]/{out = $0}END{print out}' RS="##########" file
This will save the last entry that contains something other than just white space
 
1 members found this post helpful.
Old 02-22-2011, 04:15 PM   #4
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Quote:
Originally Posted by grail View Post
Actually, as the data ends with the separator the above will not work as the last entry is the empty data after the last separator.
Maybe try:
Code:
awk '/[^[:space:]]/{out = $0}END{print out}' RS="##########" file
This will save the last entry that contains something other than just white space
Actually, the command I provided works if the '##########' is the absolute last thing in the file. If anything follows the '##########', even a newline, then you get that which may be blank.

So, yours is definitely more robust. Good one, thx for speaking up.
 
Old 02-22-2011, 07:03 PM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ ruby -0777 -lne 'print $_.split(/##+/)[-1]' file
 
Old 02-23-2011, 06:54 AM   #6
sylaan
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by grail View Post
Actually, as the data ends with the separator the above will not work as the last entry is the empty data after the last separator.
Maybe try:
Code:
awk '/[^[:space:]]/{out = $0}END{print out}' RS="##########" file
This will save the last entry that contains something other than just white space
Perfect, this worked! Initially it didn't but after testing it on another box, I realized I had some other kind of awk version installed. Installing GNU awk "fixed" it. Thank you
 
  


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
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
editing a very large HTML file (or, extracting URLs from a file) Chriswaterguy Linux - Software 3 11-27-2007 06:07 PM
extracting gz file..... b123coder Linux - Newbie 1 11-21-2004 07:55 AM
RPM Spec file creation: %file section question davidas Linux - Newbie 0 03-16-2004 10:36 PM
extracting this file... Xylicon Linux - General 4 01-23-2003 08:49 AM

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

All times are GMT -5. The time now is 09:10 AM.

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