2009-12-29 51 views
0

我會用一個例子更好地解釋我的情況。找到一個正則表達式的序列,然後找到第二個或接下來的幾行

考慮一個httpd.conf文件,我需要在其中更改文檔根目錄。因此,首先我需要找到ServerName,然後更改文檔根目錄,所以我相信在這裏我需要兩個正則表達式,但我不知道該怎麼做?有人可以幫忙嗎?或者我只需要找到ServerName並做一個備註的行號,然後繼續使用腳本查找DocumentRoot?謝謝。

+0

DocumentRoot總是在ServerName後面嗎?語法允許DocumentRoot和ServerName以任意順序指定,只要它們位於同一個VirtualHost sblock中即可。 – 2009-12-29 14:24:25

+0

是的,你是正確的,但在我的情況下,ServerName總是首先。 – mike 2009-12-29 14:42:05

回答

0

我不確定,但也許這樣的事情會爲你工作。

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

對不起,我沒有仔細閱讀你的問題......我的回答沒有幫助 – kokosing 2009-12-29 14:32:42

0

我不知道爲什麼你需要搜索服務器名第一,但這裏的你怎麼能DocumentRoot的

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

改變或者,如果你正在使用的服務器名值來創建新的根,

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

你的版本將更改配置文件中的所有DocumentRoot。這就是我需要使用ServerName找到虛擬主機條目並更改其DocumentRoot的原因。感謝您的回覆。 – mike 2009-12-29 15:11:29

相關問題