LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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: 10,007

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


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
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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