开发者

Find a sequence with regular expression then find a second one next or few line to it

I will better explain my situation with an example.

Considering a httpd.conf file in which I need to change the document root. So first I need to find the ServerName then change the document root, so I believe here I need two regexp but I m not sure how to d开发者_运维技巧o it?Can someone please help?Or do I just need to find the ServerName and make a note of the line number then proceed with finding the DocumentRoot using a script? Thanks.


I am not sure, but maybe something like this will work for you.

cat /etc/apache2/http.conf | sed 's/.*ServerName=.*/ServerName=YourNewLocation/' > tmp
mv tmp /etc/apache2/http.conf


i am not sure why you need to search for ServerName first, but here's how you can change documentroot

awk '/^DocumentRoot/{$0="DocumentRoot /new/root" } {print}' /path/httpd.conf  > temp
mv temp /path/httpd.conf 

Or if you are making use of ServerName value to create your new root,

awk '$1~/ServerName/{servername=$2}
/^DocumentRoot/{
 $0="DocumentRoot "servername  # create your new root here.
}
{print}
' /path/httpd.conf > temp
mv temp /path/httpd.conf
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜