In this post we will know about sed command to display text between two strings or keywords or words .
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) (Definition Reference from man page of sed).
Knowledge of Regex give you immense power to work with sed command.
To display text between two strings or keywords by using sed command,follow the given below Syntax :
sed -n '/Keyword-1/,/Keyword-2/p' file-name
Recently I used above syntax while migrating WordPress site to octopress.
Here is below given example of command which I used in my server. In this example, I am displaying the text between two strings that is {% codeblock %} and {% endcodeblock %} from file called 2014-06-15-post-title.markdown .
As per above given command syntax, I replace below given values.
Keyword-1 = {% codeblock %}
Keyword-2 = {% endcodeblock %}
sed -n '/{% codeblock %}/,/{% endcodeblock %}/p' source/_posts/2014-06-15-post-title.markdown
In Unix and Linux System Administration, most probably you will get chance to work on Apache server. Apache server has config called httpd.conf where we by default create VirtualHost .
To display content between VirtualHost tags , use given below example command
sed -n '//,/ /p' /etc/httpd/conf/httpd.conf
Below given is its output :
[root@mysites ~]# sed -n '//,/ /p' /etc/httpd/conf/httpd.conf#ServerAdmin webmaster@dummy-host.example.com 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
Leave a Reply