Monday, 25 June 2007

Shell: cfs - cut sort unique

Below is simple script to cut the specified field ("|" separated) from flat file, sort unique and display.

###### Start Script #############
#!/bin/sh

if [ $# -lt 1 ]
then
echo "Usage: $0 <filed_number> <filename>"
exit;
fi

cut -d"|" -f$1 $2|sort -u

###### End Script ###############

How to run
copy the contents from start script to end script and place in a file named cfs

$ chmod a+x cfs
$ copy the cfs to bin
$ cfs 1,3 filename
523|523
e23|e23
r23|r23
t23|t23
y23|y23

Shell: csu - cut sort unique with count

Below is simple script to cut the specified field ("|" separated) from flat file, sort unique and displays with count.


###### Start Script #############
#!/bin/sh

if [ $# -lt 1 ]
then
echo "Usage: $0 <filed_number> <filename>"
exit;
fi

cut -d"|" -f$1 $2|sort |uniq -c

###### End Script ###############

How to run
copy the contents from start script to end script and place in a file named csu

$ chmod a+x csu
$ copy the csu to bin
$ csu 2,3 filename
12 523|install_Mac.pdf
12 e23|install_Mac.pdf
12 r23|install_Mac.pdf
10 t23|install_Mac.pdf
2 t23|install_Win.pdf
11 y23|install_Mac.pdf
1 y23|install_Win.pdf

Perl: export table to flat file

Below is the script to extract a table from the mysql database to a flat file.

You need to change the DB_NAME, USERNAME and PASSWORD.

##### Start Script #############
#!/usr/bin/perl -w

use strict;
use DBI;
use DBD::mysql;


my ($dbh, $sql, $sth, $re, $header, $table);

if ($#ARGV ne 0)
{
print "Usage: $0 < TABLE_NAME>\n";
exit;
}

$table=$ARGV[0];

$dbh=DBI->connect('dbi:mysql:DB_NAME','USERNAME','PASSWORD',{AutoCommit => 0, RaiseError => 1}) or die "Unable to connect to DB_NAME: $dbh->err\n";

$sql=<<_KAL_;

select * from $table
_KAL_

$sth = $dbh->prepare($sql) or die "Unable to prepare $sql\n";

$sth->execute();

open(KAL, ">$table.dat") or die "Unable to open $table.dat for writing\n";
$header=$sth->{NAME};
print KAL join('|', @$header), "|\n";
while($re=$sth->fetchrow_arrayref())
{
print KAL join('|', @$re), "|\n";
}
close(KAL);

$sth->finish;

$dbh->disconnect;


##### End Script #############

How to run
copy the contents from start script to end script and place in a file named extract.pl

$ chmod a+x extract.pl
$ ./extract.pl emp
$ more emp.dat