Find and Display text between two words/strings

Find and Display text between two words/strings

You can use the sed command to find and display text between the two words/string from a file.

The following is an example to display all the contents between FOO and BAR in the file test.txt:

$ sed -n ‘/FOO/,/BAR/p’ test.txt

You may replace the above command in order to display the content between the two words of your choice:-

$ sed -n ‘/xyz/,/abc/p’ /path/to/file

This command can be helpful to find all the virtualhost entries from httpd.conf. You can format it as given below:-

$ sed -n ‘/< VirtualHost /,/< \/VirtualHost>/p’ /etc/httpd/conf/httpd.conf

Output:

#< VirtualHost *:80>
#    ServerAdmin [email protected]
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#< /VirtualHost>
< VirtualHost 1.1.1.1:80>
    ServerName gaganonthenet.com
    ServerAlias www.gaganonthenet.com
    ServerAdmin [email protected]
    DocumentRoot ######################
    ScriptAlias /cgi-bin/ ######################
< /VirtualHost>

No Comments

Post a Comment

Time limit is exhausted. Please reload CAPTCHA.