#!/usr/local/bin/perl -w #See https://confluence.slac.stanford.edu/display/IEPM/IEPM+Perl+Coding+Styles #for version of perl to use. # /*---------------------------------------------------------------*/ # /* STANFORD UNIVERSITY NOTICES FOR SLAC SOFTWARE */ # /* ON WHICH COPYRIGHT IS DISCLAIMED */ # /* */ # /* AUTHORSHIP */ # /* This software was created by , Stanford Linear */ # /* Accelerator Center, Stanford University. */ # /* */ # /* ACKNOWLEDGEMENT OF SPONSORSHIP */ # /* This software was produced by the Stanford Linear Accelerator */ # /* Center, Stanford University, under Contract DE-AC03-76SFO0515 */ # /* with the Department of Energy. */ # /* */ # /* GOVERNMENT DISCLAIMER OF LIABILITY */ # /* Neither the United States nor the United States Department of */ # /* Energy, nor any of their employees, makes any warranty, */ # /* express or implied, or assumes any legal liability or */ # /* responsibility for the accuracy, completeness, or usefulness */ # /* of any data, apparatus, product, or process disclosed, or */ # /* represents that its use would not infringe privately owned */ # /* rights. */ # /* */ # /* STANFORD DISCLAIMER OF LIABILITY */ # /* Stanford University makes no representations or warranties, */ # /* express or implied, nor assumes any liability for the use of */ # /* this software. */ # /* */ # /* STANFORD DISCLAIMER OF COPYRIGHT */ # /* Stanford University, owner of the copyright, hereby disclaims */ # /* its copyright and all other rights in this software. Hence, */ # /* anyone may freely use it for any purpose without restriction. */ # /* */ # /* MAINTENANCE OF NOTICES */ # /* In the interest of clarity regarding the origin and status of */ # /* this SLAC software, this and all the preceding Stanford */ # /* University notices are to remain affixed to any copy or */ # /* derivative of this software made or distributed by the */ # /* recipient and are to be affixed to any copy of software made */ # /* or distributed by the recipient that contains a copy or */ # /* derivative of this software. */ # /* */ # /* SLAC Software Notices, Set 4 (OTT.002a, 2004 FEB 03) */ # /*---------------------------------------------------------------*/ # Copyright (c) 2008 # The Board of Trustees of # the Leland Stanford Junior University. All Rights Reserved. ################################################################## # Author: my $authors = "Umar Kalim [kalim at slac.stanford.edu]"; my $releaseDate = "07/07/2009"; my $lastRevised = "07/07/2009"; my $version = "Authors: $authors $releaseDate, version 1.0, Last revised on $lastRevised. "; my $progname = $0 =~ s'^.*/''; ################################################################## # Used to calculate time to execute the script. #my $startTime = time(); #See sub printUsageAndExit() (at bottom) for help use strict; use Sys::Hostname; my $ipaddr = gethostbyname(hostname()); my ($hostname, $aliases, $addrtype, $length, @addrs) = gethostbyaddr($ipaddr, 2); ################################################################## #Used to remove whitespace around a string sub trim { my $string = shift; $string =~ s/^\s+|\s+$//g; return $string; } ###################Main Program#################################### my ($output_file, $input_file, $help); use Getopt::Long; GetOptions( "o|output=s" => \$output_file, "i|input=s" => \$input_file, "h|help" => \$help, ); if (defined $help) { #Placed here since needs $minimum_rtt to be defined. &printUsageAndExit; } unless (defined $output_file) { $output_file = "country_data.sql" } unless (defined $input_file) { $input_file = "country_list.csv" } #############Loading country to region mapping################## open(INFILE, $input_file) or die "Can't open file $input_file: $! \n"; my @lines = ; close INFILE or die "Can't close file $input_file: $!\n"; open(OUTFILE, ">", $output_file) or die "Can't open file >$output_file: $!\n"; my (@tempArray, $line); foreach $line (@lines) { chomp $line; ($line, undef) = split(/#/, $line); #Remove comments if (!defined($line) || $line eq "") { next; } @tempArray = split(",", $line); #Get tokens split by tabs $tempArray[1] =~ s/,/ /g; #Remove commas from country print OUTFILE "INSERT INTO `IEPM.COUNTRY` (`COUNTRY_ID`, `COUNTRY`, `CONTINENT`, `TLD`) VALUES (" . $tempArray[0] . ",\"" . $tempArray[1] . "\",\"" . $tempArray[2] . "\",\"" . $tempArray[3] . "\");\n"; # . $tempArray[4] . "\")\n"; } close OUTFILE or die "Can't close file $output_file: $!\n"; ################################################################## sub printUsageAndExit() { my $USAGE = "$progname is used to pasrse the country list and generate an SQL dump file. Usage: $progname [-i country_list.csv] [-o country_data.sql] \n"; print $USAGE; exit; }