"...and all about how music can transport people spiritually."
Dizzy Gillespie in Carr, I., Fairweather, D, Brian P, The rough guide to Jazz. page. 291.
Thursday, October 21, 2010
Tuesday, October 12, 2010
Using back references in SED
Today I have several CSV files, everyone with a header at the first line and a date at the first fields with the format mm/dd/yyyy. It was necessary to change that field into the format yyyy-mm-dd.
Here there is a Perl solution and also a SED one:
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
chomp;
if (/^[^0-9]/) {
print "$_\n";
next;
}
my @Fields = split /,/, $_, 2;
my ($Month, $Day, $Year) = split /\//, $Fields[0];
print "$Year-$Month-$Day, $Fields[1]\n";
}
__END__
Yesterday on my way home I realized that the Perl script was horrible, a one-liner should be OK, like the one below:
perl -i.bak -pe 's!(\d+)/(\d+)/(\d+)!\3-\1-\2!' *.csv
Here there is a Perl solution and also a SED one:
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
chomp;
if (/^[^0-9]/) {
print "$_\n";
next;
}
my @Fields = split /,/, $_, 2;
my ($Month, $Day, $Year) = split /\//, $Fields[0];
print "$Year-$Month-$Day, $Fields[1]\n";
}
__END__
sed -i.bak 's/^\([0-9]\+\)\/\([0-9]\+\)\/\([0-9]\+\)/\3-\1-\2/' *.csv
Yesterday on my way home I realized that the Perl script was horrible, a one-liner should be OK, like the one below:
perl -i.bak -pe 's!(\d+)/(\d+)/(\d+)!\3-\1-\2!' *.csv
Monday, October 4, 2010
Deleting Glassfish old logs
#!/bin/bash
DAYS_TO_KEEP=$((7 * 86400)) # In seconds
LOG_DIR=/var/lib/glassfishv2/domains/domain1/logs
TODAY=$(date +%s)
for FileName in $(ls ${LOG_DIR}/server.log_*)
do
FileDate=$(stat --printf=%Y ${FileName})
DateDiff=$((TODAY - FileDate))
if [ $DateDiff -gt ${DAYS_TO_KEEP} ]
then
echo Deleting ${FileName} ${FileDate} ${TODAY} ${DateDiff}
rm ${FileName}
fi
done
##END##
DAYS_TO_KEEP=$((7 * 86400)) # In seconds
LOG_DIR=/var/lib/glassfishv2/domains/domain1/logs
TODAY=$(date +%s)
for FileName in $(ls ${LOG_DIR}/server.log_*)
do
FileDate=$(stat --printf=%Y ${FileName})
DateDiff=$((TODAY - FileDate))
if [ $DateDiff -gt ${DAYS_TO_KEEP} ]
then
echo Deleting ${FileName} ${FileDate} ${TODAY} ${DateDiff}
rm ${FileName}
fi
done
##END##
Glassfish start/stop script for Ubuntu
#!/bin/bash
GF_DIR=/usr/share/glassfishv2
GF_DOMAIN_DIR=/var/lib/glassfishv2/domains/domain1
case "$1" in
start)
#${GLASSFISHHOME}/bin/asadmin start-database
${GF_DIR}/bin/asadmin start-domain --user admin --passwordfile ${GF_DOMAIN_DIR}/config/password domain1
;;
stop)
${GF_DIR}/bin/asadmin stop-domain domain1
#${GLASSFISHHOME}/bin/asadmin stop-database
;;
restart)
${GF_DIR}/bin/asadmin stop-domain domain1
#${GLASSFISHHOME}/bin/asadmin stop-database
#${GLASSFISHHOME}/bin/asadmin start-database
${GF_DIR}/bin/asadmin start-domain --user admin --passwordfile ${GF_DOMAIN_DIR}/config/passwordfile domain1
;;
*)
echo $"usage: $0 {start|stop|restart}"
exit 1
esac
##END##
GF_DIR=/usr/share/glassfishv2
GF_DOMAIN_DIR=/var/lib/glassfishv2/domains/domain1
case "$1" in
start)
#${GLASSFISHHOME}/bin/asadmin start-database
${GF_DIR}/bin/asadmin start-domain --user admin --passwordfile ${GF_DOMAIN_DIR}/config/password domain1
;;
stop)
${GF_DIR}/bin/asadmin stop-domain domain1
#${GLASSFISHHOME}/bin/asadmin stop-database
;;
restart)
${GF_DIR}/bin/asadmin stop-domain domain1
#${GLASSFISHHOME}/bin/asadmin stop-database
#${GLASSFISHHOME}/bin/asadmin start-database
${GF_DIR}/bin/asadmin start-domain --user admin --passwordfile ${GF_DOMAIN_DIR}/config/passwordfile domain1
;;
*)
echo $"usage: $0 {start|stop|restart}"
exit 1
esac
##END##
Thursday, September 30, 2010
Insert data into MySQL from a CSV file
LOAD DATA INFILE '/var/lib/mysql/btdata.csv' INTO TABLE btpl.parset FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (idsystem, name, dataurl, description);
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
mysqlimport — A Data Import Program
The mysqlimport client provides a command-line interface to the LOAD DATA INFILE SQL statement. Most options to mysqlimport correspond directly to clauses of LOAD DATA INFILE syntax.
Invoke mysqlimport like this:
shell> mysqlimport [options] db_name textfile1 [textfile2 ...]
Command invocation for a CSV file exported from Excel:
mysqlimport --fields-terminated-by=, --ignore-lines=1 -u root -p rolling mbf_symbols.csv
For each text file named on the command line, mysqlimport strips any extension from the file name and uses the result to determine the name of the table into which to import the file's contents. For example, files named patient.txt, patient.text, and patient all would be imported into a table named patient.
http://dev.mysql.com/doc/refman/5.1/en/mysqlimport.html
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
mysqlimport — A Data Import Program
The mysqlimport client provides a command-line interface to the LOAD DATA INFILE SQL statement. Most options to mysqlimport correspond directly to clauses of LOAD DATA INFILE syntax.
Invoke mysqlimport like this:
shell> mysqlimport [options] db_name textfile1 [textfile2 ...]
Command invocation for a CSV file exported from Excel:
mysqlimport --fields-terminated-by=, --ignore-lines=1 -u root -p rolling mbf_symbols.csv
For each text file named on the command line, mysqlimport strips any extension from the file name and uses the result to determine the name of the table into which to import the file's contents. For example, files named patient.txt, patient.text, and patient all would be imported into a table named patient.
http://dev.mysql.com/doc/refman/5.1/en/mysqlimport.html
Creating a filename using the output of a command in MSDOS
From Microsoft Windows XP Documentation:
Parsing output
You can use the for /F command to parse the output of a command by making the filenameset between the parenthesis a back quoted string. It is treated as a command line, which is passed to a child Cmd.exe and the output is captured into memory and parsed as if it were a file.
For example, a batch file like the one that follows:
@echo off
rem Creates the yyyymmdd.csv file from the statement text file
rem The date for the filename is taken from the statement text file
rem with a perl program called getdate.pl that returns the date throgh
rem standard output
FOR /F "usebackq" %%d IN (`getdate.pl statement.txt`) DO @statement.pl statement.txt > %%d.csv
Parsing output
You can use the for /F command to parse the output of a command by making the filenameset between the parenthesis a back quoted string. It is treated as a command line, which is passed to a child Cmd.exe and the output is captured into memory and parsed as if it were a file.
For example, a batch file like the one that follows:
@echo off
rem Creates the yyyymmdd.csv file from the statement text file
rem The date for the filename is taken from the statement text file
rem with a perl program called getdate.pl that returns the date throgh
rem standard output
FOR /F "usebackq" %%d IN (`getdate.pl statement.txt`) DO @statement.pl statement.txt > %%d.csv
Wednesday, September 29, 2010
Get public advisories from the National Hurricane Center
#!/usr/bin/perl
# Download the public advisory from National Hurricane Center
use strict;
use warnings;
use Net::FTP;
my $FtpServer = 'ftp.nhc.noaa.gov';
my $RemoteDir = '/pub/products/nhc/public';
my $LocalFileName = '/home/janeiros/chk/txt/nhc.txt';
# Get the modification time of the local file
my $LocalFileMDTM = (stat($LocalFileName))[9];
my $Ftp = Net::FTP->new($FtpServer, Passive => 1, Debug => 0)
or die "Couldn't no connect to $FtpServer: $!\n";
$Ftp->login('anonymous', 'jesus.aneiros@gmail.com')
or die "Couldn't logon to $FtpServer: $!\n";
$Ftp->cwd($RemoteDir);
$Ftp->ascii();
# Get the file name of the last advisory
my $RemoteFileName = ($Ftp->ls())[-1];
# Get the last modification time for the remote file
my $RemoteFileMDTM = $Ftp->mdtm($RemoteFileName);
# Get the file only if it is older than the local
if (!defined($LocalFileMDTM) || $RemoteFileMDTM > $LocalFileMDTM) {
$Ftp->get($RemoteFileName, $LocalFileName);
}
$Ftp->quit;
__END__
# Download the public advisory from National Hurricane Center
use strict;
use warnings;
use Net::FTP;
my $FtpServer = 'ftp.nhc.noaa.gov';
my $RemoteDir = '/pub/products/nhc/public';
my $LocalFileName = '/home/janeiros/chk/txt/nhc.txt';
# Get the modification time of the local file
my $LocalFileMDTM = (stat($LocalFileName))[9];
my $Ftp = Net::FTP->new($FtpServer, Passive => 1, Debug => 0)
or die "Couldn't no connect to $FtpServer: $!\n";
$Ftp->login('anonymous', 'jesus.aneiros@gmail.com')
or die "Couldn't logon to $FtpServer: $!\n";
$Ftp->cwd($RemoteDir);
$Ftp->ascii();
# Get the file name of the last advisory
my $RemoteFileName = ($Ftp->ls())[-1];
# Get the last modification time for the remote file
my $RemoteFileMDTM = $Ftp->mdtm($RemoteFileName);
# Get the file only if it is older than the local
if (!defined($LocalFileMDTM) || $RemoteFileMDTM > $LocalFileMDTM) {
$Ftp->get($RemoteFileName, $LocalFileName);
}
$Ftp->quit;
__END__
Subscribe to:
Posts (Atom)