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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.