Friday, May 30, 2008

[perl] Regular expressions

* Oneliner to remove spaces at the beginning and at the end of a string (trim)
$string =~ s|^(\s*)(.*?)(\s*)$|$2|s;

* Replacing "::" by "/" . This can be useful when you want to see if you can load a module at run time

$module =~ s|::|/|g;
eval "require $module.pm";

Tuesday, May 27, 2008

[mysql] - Dropping tables without caring about relationships

On Mysql, to avoid having a
"Cannot delete or update a parent row: a foreign key constraint fails"
message when dropping tables with relationships between them, you can set the flag
SET FOREIGN_KEY_CHECKS=0;
at the very beginning of your sql script.

Monday, May 19, 2008

[Tip] How to concatenate pdf files

Sometimes, I received some few documents in pdf format and I would rather read them as a whole document than to close one page and open the next one.

The tool "pdftk" can be used to concatenate the different pdf files to create 1 file containing the whole

See this tutorial on how to use it

Friday, May 2, 2008

Which Linux version am I running?

I can't never remember which version of GNU/Linux I'm running on. I know I use Ubuntu but is it Gutsy Gibbon? Hoary Hedgehog? Feisty Fawn?

I just found out about a tool called lsb_release.

lsb_release is a tool created by the LSB (Linux Standard Base) that returns information about the distribution name, version, codename... So this tool should be available on most of the GNU/Linux distribution.

The output will look like this:

cyril@merlin:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 7.10
Release: 7.10
Codename: gutsy


Time to upgrade to Hardy Heron

Tuesday, April 29, 2008

Perl - How to merge 2 simple hashes ?

How to merge 2 hashes ? with the use of hash slice:

my %hash1 = ( hello1 => 'world1' , foo1 => 'bar1', hello2 => 'foobar' );
my %hash2 = ( hello2 => 'world2' , foo2 => 'bar2' );
my %hash = %hash1; # %hash contains the same keys/values as hash1
@hash{keys %hash2} = values %hash2; # We merge the content of hash2 with hash


This uses hash slices in order to merge the content of hashes. Slices can also be used to remove duplicates entries in an array:

my @array = qw/hello world foo bar hello/;
my %hash;
@hash{@array} = undef;
my @array = keys(%hash);

Friday, April 11, 2008

Transforming Unicode

I used to work in the localisation industry (terrible world).
Once, a former colleague asked me if it would be possible to retrieve the japanese characters from the unicode string. With Perl, the answer is "YES"
Just make sure you have the module Unicode::Escape installed, save your unicode string in a text file (i.e. unicodetext.txt) and run the oneliner:

perl -MUnicode::Escape -ne 'print Unicode::Escape::unescape($_);'/path/to/unicodetext.txt

Example with some strings in russian:
With a unicode string:

\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435


the result would be:

Предисловие

Your favorite Linux command

I am subscribed on a french Linux mailing list, lea-linux.org and one of the moderator, Emmanuel F., sent this nice command to find out what your favorite command is:

history|awk '{a[$2]++ }END{for(i in a){print a[i] " " i}}'|sort -rn|head