0

基本的に、私がやりたいことは、 awk から解析された結果を別のものに置き換えることです。次の awk コマンドがあります。

awk '$1~/^DocumentRoot/{print $2}' /etc/apache2/sites-available/default

どちらが返されます:

/var/www/

What I want to do is replace the /var/www/ (of course, it can be anything and not /var/www/ in particular) in the file. I tried to pipe a sed command, but I can't figure out how to do it...

Anyone can help ?

4

2 に答える 2

2

結果をtext変数でキャッチしてから、sed -iそれをキャッチします。

text=$(awk '$1~/^DocumentRoot/{print $2}' /etc/apache2/sites-available/default)
sed -i "s#$text##g" /etc/apache2/sites-available/default

あなたのコメントに基づいて

ファイルは次のようになります: DocumentRoot /var/www しかし、次のようにすることもできます: DocumentRoot /usr/www/

sedですべてを行うことができます:

$ cat a
DocumentRoot /var/www
eee
$ sed -i 's#DocumentRoot.*#DocumentRoot hello#g' a
$ cat a
DocumentRoot hello
eee
于 2013-09-19T08:59:15.153 に答える
2

/var/www/何か別のものに置き換えたいと思います。たとえば/var/www2/、変更されたパスを表示してみましょう。これが当てはまる場合は、

awk '$1~/^DocumentRoot/{print $2="/var/log/"; print;}' /etc/apache2/sites-available/default
于 2013-09-19T08:59:22.300 に答える