Archive for the 'Perl' Category

Perl oneliner – converting several lines to a comma-separated list

Thursday, November 23rd, 2006

Sometimes I have a list of something in a file, one line per item and want to convert it to a comma(collon,…)-separated line (with no trailing separator of course) that can be used as a command-line parameter to some other tool. A perl oneliner comes in handy:

`perl -e '@_=<STDIN>; chomp(@_); print join(";",@_);' < data_file`

Perl Getopt::Long

Tuesday, November 15th, 2005

Diego pointed out that I have been doing it all wrong using standard getopt. Getopt::Long is much neater and more powerful than its predecessor.

Two pointers for how to use it: active-venture and perl.com.

Here’s my sample code: GetOptions('d=s' => \$C{'DB_DSN'}, 'u=s' => \$C{'DB_USER'}, 'p=s' => \$C{'DB_PASS'}, 'c' => \$C{'DB_CREATE'}, 't=s' => \$C{'DB_TABLE'}, 's=s' => \$C{'SEP'}, 'q:s' => sub { $mode = 2; $C{'DB_QUERY'}=$[1] if ($[1] ne '');}, 'i' => sub { $mode = 1; } ) or usage();

  • Probably the most comprehensive and the best library to manipulate (also write) EXIF tags in images: Image::ExifTool (0)

Installing Perl modules

Friday, October 28th, 2005

As always with perl, there’s a couple of options ;-)

  1. Automatically using CPAN perl -MCPAN -e shell install Perl::Module::You::Want

  2. Automatically using g-cpan in Gentoo g-cpan -i Perl::Module::You::Want

  3. Semi automatically in Debian

    • find a “lib-module-you-want-perl” module that provides the package you want.
    • command apt cache search “lib.*-perl” gives you a list of all prepackaged perl modules.
    • install “apt-get install”
  4. By hand, described here.

Multiple matches in one line – perl one-liner

Friday, October 14th, 2005

(thanks to Diego): perl -ne ‘while ((/condition/g)) { print “something”; }’

Deleting unused config files in Debian

Thursday, October 13th, 2005
  • Getting a list of unused packages (not the installed ones): dpkg –list | grep -v -E “^ii”

  • Purging packages list: dpkg –purge packagename

So doing all this in one line would be something like: dpkg –purge dpkg --list | grep -v -E "^ii" | perl -ne '/^rc\s+([^\s]+)/ && print "$1 ";'

Be careful!

Counting a number of modified lines in a patch file (per file)

Tuesday, August 23rd, 2005

Just a neat one liner:

perl -ne ‘if (/^diff .* ([^\s]+)$/ || eof()) { print “$file $plus $minus\n”; $file = $1; $plus = 0; $minus = 0;} /^+/ && $plus++; /^-/ && $minus–;’

Parsing Bro’s connection logs and writing it into a database

Wednesday, August 17th, 2005
  1. Create a database table with the following fields:
    • sip address
    • sport
    • dip address
    • dport -sbytes -rbytes -conn length

create table conn(sip inet, sport int, dip inet, dport int, rbytes int, sbytes int, length float);

  1. Use perl one liner: perl -ne ‘ @a = split; print “INSERT INTO conn VALUES(‘”‘”‘$a[2]‘”‘”‘, $a[5], ‘”‘”‘$a[3]‘”‘”‘, $a[6], $a[8], $a[9],$a[1]);\n”;’

BTW need also to replace ? with NULLs — do it with =~ s/// command.

  1. Combine all of them into a horrible thing: cat conn.log | perl -ne ‘@a = split; $a[8] =~ s/\?/NULL/; $a[9] =~ s/\?/NULL/; $a[1] =~ s/\?/NULL/; print “INSERT INTO conn VALUES(‘”‘”‘$a[2]‘”‘”‘, $a[5], ‘”‘”‘$a[3]‘”‘”‘, $a[6], $a[8], $a[9], $a[1]);\n”;’ | psql test test -h 127.0.0.1

Printing a single quote in a shell script

Wednesday, August 17th, 2005

Diego’s “shell nasty quoting tricks”: print ” ‘ ” ‘ ” ‘ ” ‘

or in more details: perl -e ‘print ” a’ ” ‘ ” ‘b ” ‘