LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-18-2015, 11:50 AM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
extract text with sed.


My file, dummy.txt, reads as follows.

Quote:
AAA
blah
x
y
z
ZZZ
BBB
blah
x
y
z
ZZZ
It is two 'blocks' of text. Each block is enclosed by two regular expressions. The end of each block is always the same. In this case ZZZ, but the start of each block can vary.

Using sed to print lines between two regular expressions. Getting the lines between AAA and ZZZ, inclusive, works with the follow sed command.
Quote:
> cat dummy.txt | sed -n '/AAA/,$p; /ZZZ/q'
AAA
blah
x
y
z
ZZZ
Getting the lines between BBB and the next ZZZ does not work using exact syntax.
Quote:
> cat dummy.txt | sed -n '/BBB/,$p; /ZZZ/q'
Nothing is output with the second command but I was expecting this.
Quote:
BBB
blah
x
y
z
ZZZ
Apparently my sed command doesn't like it if the second expression is found before the first expression. By changing the 'q' to a 'd' in the sed command I am able to extract the second block.

Quote:
> cat dummy.txt | sed -n '/BBB/,$p; /ZZZ/d'
BBB
blah
x
y
z
ZZZ
Unfortunately this syntax breaks when extracting the first block.

Quote:
> cat dummy.txt | sed -n '/AAA/,$p; /ZZZ/d'
AAA
blah
x
y
z
ZZZ
BBB
blah
x
y
z
ZZZ
I suppose the 'd' is telling sed to find the last occurrence of the regular expression. Since this is part of a loop the same syntax is needed for all blocks.

My real file has more blocks, 24, and the expressions are not as simple but the concept is the same.
 
Old 09-18-2015, 11:56 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
d means delete the match.

What you really want is this (and for sanity's sake, drop the cat):

Code:
sed -n '/AAA/,/ZZZ/p' dummy.txt
and

Code:
sed -n '/BBB/,/ZZZ/p' dummy.txt
-n - does not echo each line as it is read
p - print match
 
1 members found this post helpful.
Old 09-18-2015, 12:44 PM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by goumba View Post
d means delete the match.

What you really want is this (and for sanity's sake, drop the cat):

Code:
sed -n '/AAA/,/ZZZ/p' dummy.txt
and

Code:
sed -n '/BBB/,/ZZZ/p' dummy.txt
-n - does not echo each line as it is read
p - print match
Yep. That did it.
Thanks,
 
  


Reply

Tags
sed



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] How to replace text using sed or awk in a file without changing the commented text coolpraz Programming 4 01-03-2013 06:19 AM
[SOLVED] sed use - extract substring from lines of text file tastiero Linux - Newbie 26 03-12-2012 04:10 AM
using sed or grep to extract stuff from a text file DEF. Programming 5 12-12-2009 10:13 AM
Extract certain text info from text file xmrkite Linux - Software 30 02-26-2008 11:06 AM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 01:40 AM

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

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