Sed Find and Display Text Between Two Strings or Words

by Vivek Gite · 17 comments

Q. How do I find the text between the strings FOO and BAR inclusive using sed command line option?

A. Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream - a file or input from a pipeline.

To output all the text from file called test.xt' between 'FOO' and 'BAR', type the following command at a shell prompt. The -n option suppress automatic printing of pattern space:
$ sed -n '/WORD1/,/WORD2/p' /path/to/file
$ sed -n '/FOO/,/BAR/p' test.txt

You can easily find out all virtual host entries from httpd.conf, type
# sed -n '/<VirtualHost*/,/<\/VirtualHost>/p' /etc/httpd/conf/httpd.conf

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 17 comments… read them below or add one }

1 kaosmonk April 14, 2008

This is just what I’ve needed! I’ve been playing with awk these days, trying to do the same. As these are my first steps in text manipulation field, I’d appreciate if you could help me out with problems I’ve encoutered: e.g. I want to print only the substring between two words/strings and not those two words/strings (the command you gave prints WORD1 and WORD2, and I do not want them to be printed at all); I believe it’s possible to do some search&replace after I run the above sed command, but I wonder if it’s possible to be done through only one iteration; secondly, let’s say I have two files with different strings, where each string is in its own line in the file; what I want to do is to append each string of the second file to the first so that I could have a file formatted like this:
first_file’s_string1, second_file’s_string1
first_file’s_string2, second_file’s_string2

I couldn’t find a way to do this no matter what I’ve tried. Can someone help me with this? Would appreciate a lot!

Reply

2 Binny V A April 14, 2008

, is a range operator in sed – its very useful. I think it can be used to specify 2 line numbers and it will return all the lines between those 2 lines.

Reply

3 vivek April 14, 2008

Yes, it is a range operator; if you know line number it is good; but most time you need to select data using dynamic conditions

Reply

4 gameboy April 22, 2008

Hi,
thanks it’s a good tips !!
I notice a little mistake in your regular expression :
“/<VirtualHost*/”
means <VirtualHost with 0 t or an infinite t

It will be better :
“/<VirtualHost.*/”

see ya

Reply

5 Gagan Brahmi May 5, 2008

A better option will be use to ‘space’ after VirtualHost string in order to ensure that we get the VirtualHost having some value against it.

For example:-

# sed -n ‘/<VirtualHost /,//p’ /etc/httpd/conf/httpd.conf

Reply

6 Robsteranium June 5, 2008

I arrived here struggling with this one but I’ve just figured it out!

/WORD1/,/WORD2/{
/WORD1/d
/WORD2/d
p
}

hth

Reply

7 yoander June 11, 2008

Very useful tip about sed

Reply

8 reza September 10, 2008

hi
i have a file like this:

ther: Part II (1974) …. Vito Corleone
… aka Mario Puzo’s The Godfather: Part II (USA: complete title)
# Mean Streets (1973) …. Johnny Boy
# Bang the Drum Slowly (1973) …. Bruce Pearson
# The Gang That

I need to delete all between ‘)’ and the ‘#’ (keep the # start of each line as is)
output:

ther: Part II (1974)
# Mean Streets (1973)
# Bang the Drum Slowly (1973)
# The Gang That
….

how do i do it?

Reply

9 codegazer May 1, 2009

Hi,
Have been reading your blogs for quite some time now and are very informative.
On this particular script, I need some advice. Say I have 2 words, Begin and End that I am looking for in a file. For Ex.

Begin
line1
line2
End

I am trying to modify your script. I want to delete what lies between these 2 patterns viz. Begin/End but I need to keep the words Begin and End. I end up deleting those as well.

Any inputs ?

Thanks

Reply

10 sedbeginner May 14, 2009

I have a similar problem, I have a long line of text and within this line I have my start and end variable and I need the text between start and end.

Example:

t6gd68g d9d8j5%9j30j 0jf087*(&&^*2hd920STARTid8 =e72920 2d9nf9END93nf300j90

needed output between START and END, resulting in id8 =e72920 2d9nf9

Any Idea how to do this?

Cheers.

Reply

11 Nico Maas June 17, 2009

Hi there,
I got some problem.
I got this html Code:

1 neue Nachricht

And need to extract the Text “1 neue Nachricht” – but that thing can change.
Any way in doing this by sed?

Thanks

Reply

12 Nico Maas June 17, 2009

I meant this Code:

a href=”../messages/index.php” class=”navilink”>1 neue Nachricht

Reply

13 Robsteranium June 17, 2009

You may find that a web scraper using xpath is more appropriate for extracting data from web pages. Try ScRubyt!

Reply

14 dinu August 21, 2009

Shell script to print contents of file from given line number to next given number of lines

Reply

15 Rajiv January 7, 2010

I have scenario where i need to copy all the lines in a logfile between 2 specific time intervals. say 5 PM to 6 PM, as the file is quite huge in size we are unable to open the file. please suggest a work around .

Thanks
Rajiv.

Reply

16 AVKLINUX June 24, 2010

what if I only want to match 2 strings for 1st instance . ?

not globally.

THANKS
AVK

Reply

17 Alex July 7, 2010

Hello,
What if I have a txt like this:
Begin
yyyy
yyyy
End
…….
Begin
xxxx
xxxx
End

what if I want to display the text between “begin” and “end” but only for the first sequence, or only for the 2nd or 3rd?
Thanks a lot.

Reply

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: