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

Posted at May 17, 2005 08:53 PM



Archives
December 2005
September 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
December 2002
November 2002
October 2002
September 2002
August 2002
July 2002
June 2002
May 2002
April 2002
March 2002
February 2002
January 2002
Recent Entries
Books '05
Eco, Truman
Quotes
Zen koan
Illiberal Democracy
The Cold War vs today
Money
Life stories
Swim with the fishes
Blink of an eye
Categories
aphorisms
books
business
general
history
misc
philosophy
pop-culture
quotes
tech
Blogs
Metafilter
Arts and Letters Daily
Sapta-Sindhu
Beemblog
dineoutindia
Sponsorship