About Linux FAQ

Browse More FAQs:

Sed Find and Display Text Between Two Strings or Words

Posted by Vivek on Saturday April 12, 08 @8:21 pm

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

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Linux / UNIX FAQ:

Discussion on This FAQ

  1. kaosmonk Says:

    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!

  2. Binny V A Says:

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

  3. vivek Says:

    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

  4. gameboy Says:

    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

  5. Gagan Brahmi Says:

    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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , ~ Last updated on: April 17, 2008

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.