Warning: include_once(/home/tabrez/www/talk/mtrefsearch.php) [function.include-once]: failed to open stream: No such file or directory in /home/tabrezsyed/mandalivia.com/talk/archives/000708.php on line 118
Warning: include_once() [function.include]: Failed opening '/home/tabrez/www/talk/mtrefsearch.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/tabrezsyed/mandalivia.com/talk/archives/000708.php on line 118
May 17, 2005
Short simple scripts
Every now and then I get to write some scripts to fetch the required data from a file. These days I'm having to deal with the Sybase interfaces file. I need to translate from a server name to the host and port.
This file can be large and the format is of the form:
SERVER
query tcp ether host1 1234
master tcp ether host1 1234
Grepping with a multi-line regex can be a pain. So here is one solution:
awk '/SERVER/{f=1}f>0{f++};f>2{print$4" "$5;exit}' interfaces
Wait. On Sun systems Sybase sometimes uses TLI for the host/port so the file can look like this:
SERVER
query tli /dev/tcp \x000208fca60e123f0000000000000000
master tli /dev/tcp \x000208fca60e123f0000000000000000
The following awk script can handle this case:
awk '/SERVEr/{f=1}f>0{f++}f>2{ if($2=="tli") print $5; else print $4:; exit}' $SYBASE/interfaces
Finally we need to convert back from the TLI format to host/port. Here is some javascript that can do that (assuming val has the string).
port = parseInt(val.substring(2,6),16);
host = parseInt(val.substring(6,8),16)
+"."+parseInt(val.substring(8,10),16)+"." + parseInt(val.substring(10,12),16)+"." + parseInt(val.substring(12,14),16);
print ("host "+host+" port "+port);
And if you decide you want to filter crontab files for a certain program here is something:
crontab -l | sed -n -e '/^#/d' -e '/string/p' | awk '{print $6}'
References:
sed FAQ, version 014
Awk reference