views:

87

answers:

1

I'm working to replace a large set of TTLs inside some tinydns zonefiles. Regardless of what the TTL is currently set to, I want it to become 900. I'm looking for some sed magic, and I'm almost there it seems.

I have about 50 hostnames to replace the TTL, and the zones are sporadically assigned.

The lines in the zonefiles don't need to contain a TTL. If they don't have it, the TTL will need to be appended. In other words, the lines that I will be searching through look as follows:

^.*hostname:(hostname|IP)[:#]$

e.g.

+abc.com:12.12.12.12
=*.abc.com:x.y.z.com:1200

So I need help with constructing the regular expression for my sed command. It's going to be something like:

cat hosts.txt | while read host; do
  sed -i 's/'$host':([^:]*)(?:[0-9]*)/'$host':\1:900/' zonefile*
done

Any sed gurus feeling generous?

+4  A: 

regex is not always the best way to do some tasks. your data file contains structured information (ie delimited by some constant character, like the ":" ). therefore it is easier to manipulate using fields. here's an awk solution

awk 'BEGIN{ OFS=FS=":"}NF<3{ $0=$0":"900}NF>=3{ $NF=900}1' file

Don't need to spend time constructing your regex.

ghostdog74