Showing posts with label bash scripting. Show all posts
Showing posts with label bash scripting. Show all posts

Sunday, November 25, 2007

Multiline search and replace using sed

I had to code in to remove a host entry from hosts.cfg file of nagios. Here is a bit of superb code to do so..

sed '/#/{:a;N;/\}/!ba;/212.217.202.112/s/#.*\}//;}' hosts.cfg

That will remove the entire block starting with "# '212.217.202.112' host definition" and ending in "}"

Obfuscated code eh ? Not so..learn about registers in sed!!

Tuesday, December 12, 2006

Sorting IPs in bash scripting

Oh yeah..This is something I had my head banging for a few minutes, till I figured out the -t option of sort. Okay..I had a list of IPs around 20000 today, for which I had to sort to find any patterns so that I can block the subnet in the firewall itself. I ended up with

sort -u -n -t. -k 1,1 -k 2,2 -k 3,3 -k 4,4 /etc/eximblacks

-k and -t were the key options you need to look in the menu. There are more than one way to do it :-).

That helped me. Hope it may help you at some point of time.