LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-18-2016, 12:27 PM   #1
victordcweb
LQ Newbie
 
Registered: Feb 2014
Posts: 4

Rep: Reputation: Disabled
Grep out an entire content up to a blank line (as a delimiter)


I'm trying to grep out an entire content up to a blank line. Any help will be appreciated. I'll prefer a grep command but I'm okay with other solution

Have: File

TableA has the following
GET TABLE TABLEA
(value(1)
)
/

TABLEB has the following
GET TABLE TABLEB
(value(1)
,value(2)
)
/


WANT

An output to contain the following

TABLEA has the following
GET TABLE TABLEA
(value(1)
)
/
 
Old 10-18-2016, 12:37 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,120

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
i would process the file twice. the first time to grep the line number of the first blank line; then 2nd use head to print up to that line.

Last edited by schneidz; 10-18-2016 at 12:38 PM.
 
Old 10-18-2016, 12:55 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
awk would work just fine if you set the RS delimiter to your blank line
 
1 members found this post helpful.
Old 10-18-2016, 02:35 PM   #4
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 370

Rep: Reputation: 170Reputation: 170
With awk:
Code:
awk 'NF==0 {pr=0} /TABLEB/ {pr=1} pr' file
or passed as a parameter
Code:
awk 'NF==0 {pr=0} $0~search {pr=1} pr' search="TABLEB" file
 
Old 10-18-2016, 03:06 PM   #5
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,087
Blog Entries: 3

Rep: Reputation: 926Reputation: 926Reputation: 926Reputation: 926Reputation: 926Reputation: 926Reputation: 926Reputation: 926
And just to throw in a "sed" variant, you could try

Code:
sed -n '/^TABLEA/,/^$/{/^$/!p}' < infile.txt
Just remember that it is case sensitive.

sed -n '/A/,/B/{/C/!p}'

The -n means by default don't print anything
The /A/,/B/ { } means from pattern A to pattern B, inclusive, do what's in the braces.
The /C/!p means except when pattern C occurs, print the current pattern space (line)

Last edited by Turbocapitalist; 10-18-2016 at 03:23 PM.
 
Old 10-18-2016, 05:29 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
With Grep and Perl regexp extension:
Code:
grep -Pzo '(?s)TableA.*?\n$' file.txt
Edit: not sure if you want to grep 'TableA' or 'TABLEA'
Maybe add i Perl modifier
Code:
grep -Pzo '(?si)TableA.*?\n$' file.txt

Last edited by keefaz; 10-18-2016 at 05:33 PM.
 
Old 10-18-2016, 06:31 PM   #7
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 2,593

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
The last character (nonprinting) of a line before a blank line is newline "\n". You should use awk to print everything up to the first newline character.
 
Old 10-19-2016, 04:58 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Code:
awk '/TABLEA/' RS="" file
 
1 members found this post helpful.
  


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
LXer: Different methods to change the delimiter in file content LXer Syndicated Linux News 0 07-07-2013 01:10 PM
How do i make getline move to next line in a file after spotting a delimiter baronobeefdip Programming 3 09-11-2012 11:42 AM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
getline delimiter + move to next line? blizunt7 Programming 3 07-09-2005 12:08 AM
Blank line - grep. liguorir Linux - Software 1 04-24-2004 09:03 AM


All times are GMT -5. The time now is 04:24 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration