Archive for 2007

MySQL: quick replacement

Thursday, December 6th, 2007

Say that you have a table called myTable with several rows and a column called URL that contains the IP address of a log server. Now imagine that you want to change the value of URL so IP address is substituted by the server name. One way to do that is as follows:

use dbname

update myTbl set URL=replace(URL,’172.16.1.10′,’logserver’);

OpenSSH: simplifying logins

Thursday, December 6th, 2007

OpenSSH provides a per-user configuration file usually located in ~/.ssh/config. This file can help ease your life as Sysadmin. Let’s say that you frequently connect to a server in this way:

ssh admin@boring-servername.boring-domain.com

With OpenSSH there is an easy way to do the job. Edit or create a config file:

vi ~/.ssh/config

and then add the following:

Host servername
     User admin
     HostName boring-servername.boring-domain.com

Where Host is the alias for the remote server you want to connect to, HostName is the full name of the remote server and User is the login name.

References:

KarKomaOnline redesign

Sunday, December 2nd, 2007

I finally have the time to dedicate in moving KarkomaOnline to the WordPress content manager and made some changes to the look&feel. As always your feedback and posts are very welcome.

Hope you enjoy!

A note for registered users: I’m not going to port the accounts and settings from the old site as this implies a lot of work for me. Please take a minute to register again. Sorry for the inconvenience.

Perl on the command line

Sunday, May 27th, 2007

Perl, you know perl, is not just a jewel for scripting, it ‘s also a potent onliner.

The magic trick is the diamond:

perl -e ‘while (){print}’ file1

says perl “keep on as long as you read something in the standard in”, in the case of the example above, “print the line” … and each line goes in the memory but not the whole file: you have a powerful stream editor.

The ” -e” switch says to perl that what follows in the command line between the single quotations is a script and not a script file name.

But you can have it quicklier :

perl -ane ‘print’ file1

The switches “-ane” do the “while stuff” for you. It says “do a while () loop” (-n) “and split each record in the @F array” (-a), I haven’t used the array yet.
Perl has a difficulty : you can implicit everything. Some guys abuse of it in scripting, like spirits, but it’s good for an onliner.

This line will print file1. Great (in fact implicit in “print $_” ; $_ is the variable where perl puts what you do not explicit). Printing is great but let do some awkish things :

perl -ane ‘/$m[aiy]/ && print $_’

That will print the lines that contain $ma, $mi or $my, (have a look at the article from karkoma about sed), patterns in perl are like sed’s ones but larger. Take a look at the tutorial of perl for pattern or type “perldoc perlreftut” if you have installed perldoc.

Well, just let do some sedish thinks :

perl -ane ‘s/

]*)>//; s/
/
/;print’ file1.html

replaces the paragraph in html( by newline () but keeps the options of the paragraph ($1, first match, here the parenthesis are not optional) in an html comment.

perl -F”:” -ane ‘/pascal/ && print $F[2]‘ /etc/passwd

will print the UID of user pascal (third field), the field separator is “:” (-F$-1òý:òý)

perl -ane ‘/$m[aiy][^ s]*/ && print “line : $. variable : $&n”‘

will print the number of line ($.) where the variables that begin with “$ma”, “$mi” o “$my” and the variables that have matched the description ($&)

bad news, you received this bad MS file with carriage return (r) and newline and you want only the newline :

perl -pi.old -ane ‘s/rn$/n/;print;’ file1

and I obtain directly a file1 without carriage return and I have a backup of the original in file1.old (-pi.old)
you can have a look to a quick look at some useful perl onliners.