search and replace pattern on specific column

using gsub – replace on second column localhost per bar

awk '{ gsub( "localhost","bar",$2 ) }1' /etc/hosts

using ternary operator – if column two is equal localhost replace it per bar

awk '{ $2 = $2=="localhost" ? "bar" : $2 }'1 /etc/hosts