Sed Find and Display Text Between Two Strings or Words
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:
- How to: Create Files in Linux from a Bash Shell Prompt
- Sun Solaris UNIX display list for loaded kenel device driver / modules
- Howto Display text files in a shell script
- Linux script to prompt for password
- Linux display or change a pre-login message - /etc/issue file
Discussion on This FAQ
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!
Tags: paragraph, regular expressions, sed command, sed delete lines between pattern, sed delete lines between words, sed regular expressions, stream editor ~ Last updated on: April 17, 2008



April 14th, 2008 (5 weeks ago) at 7:00 am
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!
April 14th, 2008 (5 weeks ago) at 7:38 pm
, 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.
April 14th, 2008 (5 weeks ago) at 7:49 pm
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
April 22nd, 2008 (3 weeks ago) at 11:05 am
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
May 5th, 2008 (2 weeks ago) at 2:12 am
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