Thursday, February 19, 2009

AWK - Replacing Strings in columns

Say we have a hosts file map on a NIS based network (or even a simple hosts file for a sake of simplicity) the network department decided to change one of the VLANS (for example from 100 to 200) as part of some infrastructure organizational change. We have thousands of entries that require update:

the host file looks like this:

10.100.5.1 server1.domain.com server1
10.100.5.2 server2.domain.com server2
10.100.5.3 server3.domain.com server3

awk '{gsub("100","200");print}' /etc/hosts

awk will acomplish this task as a charm, simple - yet very powerful editor.


No comments: