Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 837273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:07:09+00:00 2026-05-15T05:07:09+00:00

I have a .SQL file containing a large number of queries. They are being

  • 0

I have a .SQL file containing a large number of queries. They are being run against a database containing data for multiple states over multiple years. The machine I am running this on can only handle running the queries for one state, in one year, at a time.

I am trying to create a Perl script that takes user input for the state abbreviation, the state id number, and the year. It then creates a directory for that state and year. Then it opens the “base” .SQL file and searches and replaces the base state id and year with the user input, and saves this new .SQL file to the created directory.

The current script I have (below) stops at

open(IN,'<$infile')

with

"Can't open [filename]"

It seems that it is having difficulty finding or opening the .SQL file. I have quadruple-checked to make sure the paths are correct, and I have even tried replacing the

$path

with an absolute path for the base file. If it was having trouble with creating the new file I’d have more direction, but since it can’t find/open the base file I do not know how to proceed.

 #!/usr/local/bin/perl

use Cwd;
$path = getcwd();
#Cleans up the path
$path =~ s/\\/\//sg;

#User inputs
print "What is the 2 letter state abbreviation for the state? Ex. 'GA'\n";
$stlet = <>;
print "What is the 2 digit state abbreviation for the state? Ex. '13'\n";
$stdig = <>;
print "What four-digit year are you doing the calculations for? Ex. '2008'\n";
$year = <>;

chomp $stlet;
chomp $stdig;
chomp $year;

#Creates the directory
mkdir($stlet);
$new = $path."\/".$stlet;
mkdir("$new/$year");

$infile = '$path/Base/TABLE_1-26.sql';
$outfile = '$path/$stlet/$year/TABLE_1-26.sql';

open(IN,'<$infile') or die "Can't open $infile: $!\n";
open(OUT,">$infile2") or die "Can't open $outfile: $!\n";
print "Working...";

while (my $search = <IN>) {
chomp $search;
$search =~ s/WHERE pop.grp  = 132008/WHERE pop.grp  = $stdig$year/g;

print OUT "$search\n";  
}
close(IN);

close(OUT);

I know I also probably need to tweak the regular expression some, but I’m trying to take things one at a time. This is my first Perl script, and I haven’t really been able to find anything that handles .SQL files like this that I can understand.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:07:10+00:00Added an answer on May 15, 2026 at 5:07 am
    $infile = '$path/Base/TABLE_1-26.sql';
    

    The string in that line is single quoted, so $path won’t interpolate, so your program is looking for a file literally named $path/Base/TABLE_1-26.sql.

    You want

    $infile = "$path/Base/TABLE_1-26.sql";
    

    Or, better,

    use File::Spec;
    ....
    $infile = File::Spec->catfile($path, 'Base', 'TABLE_1-26.sql');
    

    and similarly for $outfile — or was that $infile2 ? 🙂 I’d strongly recommend putting a use strict; and use warnings; at the top of this and your future scripts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an export SQL file containing tables and data from MySQL and I
I have a .sql file from a MySQL dump containing tables, definitions and data
Is there a way to have an application access a SQL Compact database file
I have written a PHP script which generates an SQL file containing all tables
I have a database with 1000 records containing a file name and a file
I have a sql file that creates a database in mysql: SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
If I have exported a .sql file with my database in it, can I
So I have an SQL dump file that needs to be loaded using mysql_query().
I've got two PostgreSQL databases that have been created using the same sql file.
I have a full MS SQL Backup file that I would like to extract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.