Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace Entire line using Sed 1

Status
Not open for further replies.

stu78

Programmer
Joined
May 29, 2002
Messages
121
Location
GB
Hi,

I need to comment the following line in /etc/hosts using sed,

12.9.9.0 host.internal.com host

How is this possible? For me, I cannot do a replace of 12.9.9.0 as this is not unique within hosts.

Any Ideas?
 
as this is not unique within hosts
You have some criteria to distinguish the occurrences ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I have thought of using a sed address, however, this seems a bit of overkill? Can I just not find & replace an entire line using SED?
 
Or, If I can just replace
12.9.9.0 host.

with

#12.9.9.0 host.

This would also work, I have tried;

sed -e s/12.9.9.0 host./#12.9.9.0 host./g magnus.conf > hosts > tmp && mv tmp hosts

But this does not seem to work.

 
Have you tried something like this ?
t=`echo "\t\c"`
sed '/^12\.9\.9\.0'"[ $t]*host.internal.com[ $t]*host/s!^!#!" /etc/hosts > output

The awk way:
awk '$1=="12.9.9.0" && $2=="host.internal.com" && $3=="host"{print "#"$0;next}{print}' /etc/hosts > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

Good idea with the awk - I modified it slightly to be;

awk '/12.9.9.0/ && /host.internal.com/ {print "#"$0;next}{print}' hosts > output && mv output
hosts

and this works good.

Thanks PHV.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top