2016-11-03 10 views
0

私が含まれているファイルがあります。今、私はそれを見てするようにライン「DocumentRoot /var/www/html/文字列を挿入し

下の単語「world」を追加しよう

hello 
test1 
DocumentRoot /var/www/html/ 
test2 
hey 

をこのように:上記の行は、私はこれをしようとしたが、それディ 「/」が含まれているため

hello 
test1 
DocumentRoot /var/www/html/ 
world 
test2 
hey 

それは私のために難しいです動作しないd次の

sed 's/DocumentRoot /var/#www/#html/#\ /DocumentRoot /var/#www/#html/#\nworld/' file 

をそれは完全に間違って出力

hallo 
test1 
var/var/www/html/ 
test2 
hey 
+4

'sed '@regex @ replace @'' – sat

+1

SOのドキュメントに基づいて質問を閉じることができるはずです:P https://stackoverflow.com/documentation/sed/1096/substitution/12280/using-different-delimiters#t = 201611030947028728011 – Sundeep

+0

解決策を検索しようとしましたか、この質問は頻繁に尋ねられますか? – 123

答えて

2

変更に私にスラッシュ以外の文字を区切り文字を与え、文字列を追加するaのコマンドを使用します。

sed '\#DocumentRoot /var/www/html/#a\ 
world' file 

ここでは、区切り文字として#を使用しています。 sedのいくつかのバージョンでは、これをすべて同じ行に置くことができますが、私が書いた方法はより移植性があります。

+1

は、最初に '' \ ''を追加することでマッチングに使用できる別の区切り文字も知らなかった! ++ – Sundeep

+1

@Sundeep私はいつもそれをやる方法を忘れています。これは[sed replaceでスラッシュを使用する](http://stackoverflow.com/a/5864155/1983854)に記載されています。 [sedのドキュメント](https://stackoverflow.com/documentation/sed/)に追加してください。 – fedorqui

+0

gnu sed man page - https://www.gnu.org/software/sed/manual/sed.html#sed-Programs ... ''%sedを使って行を選択する '' \%regexp% 'を参照してください。 – Sundeep

関連する問題