Yanis Boucherit
Yanis Boucherit

Reputation: 714

Replace result from awk

Basically, what I want to do is replacing a result parsed from awk with something else. I have the following awk command :

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

Which returns :

/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 ?

Upvotes: 0

Views: 145

Answers (2)

lc2817
lc2817

Reputation: 3742

I guess you want to replace /var/www/ by something else, let's say /var/www2/ and display the path modified. If this is the case go for

awk '$1~/^DocumentRoot/{print $2="/var/log/"; print;}' /etc/apache2/sites-available/default

Upvotes: 2

fedorqui
fedorqui

Reputation: 289835

Catch the result in the text variable and then sed -i it.

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

Based on your comment

the file is like this : DocumentRoot /var/www But it also can be like : DocumentRoot /usr/www/

You can do everything with sed:

$ cat a
DocumentRoot /var/www
eee
$ sed -i 's#DocumentRoot.*#DocumentRoot hello#g' a
$ cat a
DocumentRoot hello
eee

Upvotes: 2

Related Questions