Solved

How to correct Perl Input file error?

Posted on 2013-12-10
20
590 Views
Last Modified: 2013-12-15
After running the following command:
perl splitfiles.pl -i SSTEST*.txt -c test.conf -e error.txt -r report.txt
I get the following error at 1ine 132:
Unable to open SSTEST*.txt at splitfiles.pl line 132.

Below are lines 130, 131, 132 and Configuration Section
line 130      # open files
line 131     my $input_fh;  # *test+1 9/11 206168
line 132    open($input_fh, "<", $INPUT_FILE) or die "Unable to open $INPUT_FILE";
File pattern which needs to split are SSTEST.5 digits.9 digits.YYYYMMDD.835

# Configuration Section
# The users can define defaults that they want to use
$INPUT_FILE  = "test.txt";
$ERR_OUTFILE = "error.txt";
$CONFIG_FILE = "remit.conf";
$ORPHAN_FILE = "orphan.txt";

Also I need to run this from crontab.
0
Comment
Question by:jamar49
  • 9
  • 7
  • 2
  • +2
20 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
"File pattern which needs to split are SSTEST.5 digits.9 digits.YYYYMMDD.835"
No .txt? Your pattern is SSTEST*.txt but the example you gave has no .txt
Do you just want SSTEST*? Is there anything else that starts SSTEST that you don't want to split?
0
 

Author Comment

by:jamar49
Comment Utility
File pattern which needs to split are SSTEST.5 digits.9 digits.YYYYMMDD.835 with that being said should convert .835 to .txt then run perl script
0
 

Author Comment

by:jamar49
Comment Utility
There isn't anything else that starts SSTEST that I don't want to split
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Is there any other place where $INPUT_FILE  is set?
$INPUT_FILE  = "test.txt";
 open($input_fh, "<", $INPUT_FILE) or die "Unable to open $INPUT_FILE";
would not produce the message
Unable to open SSTEST*.txt at splitfiles.pl line 132.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 125 total points
Comment Utility
Change

open($input_fh, "<", $INPUT_FILE) or die "Unable to open $INPUT_FILE";

to

open($input_fh, "<", $INPUT_FILE) or die "Unable to open $INPUT_FILE because $!\n";

I suspect you have a permission issue.
0
 

Author Comment

by:jamar49
Comment Utility
permission is 775
Hereare other places where $INPUT_FILE  is set:

# Configuration Section
# The users can define defaults that they want to use
$INPUT_FILE  = "test.txt";

sub usage {
    print "Usage: $0 [-c configuration file] [-i <Input file>] [-e <error file>] [-r <reporting file>] [-s 0] [-f 1] [-o <orphaned claims>] [-d <detailed report>] [-rc <report by check>] [-p <split by payor>] [-n 1] [-sc <allow check type switch>] \n";    #*BJK 1/10 167916
    exit(1);
}

if (@ARGV>0) {     # process command-line arguments
    my $i;

    if ((@ARGV % 2) != 0) { usage()};  # error out if the number of arguments is odd
   
    for ($i=0; ($i<@ARGV+0);) {
        if ($ARGV[$i] eq "-c") {
            $i++;
            $CONFIG_FILE = $ARGV[$i++];
        } elsif ($ARGV[$i] eq "-i") {
            $i++;
            $INPUT_FILE = $ARGV[$i++];


# read the config file and return the number of lines
sub readconfig {
    my $confFile  = shift();
    my $line      = 0;
    my $index     = 0;
    my @timelist  = localtime;
    my $Time      = sprintf("%02d", ($timelist[4]+1)).sprintf("%02d", $timelist[3]).sprintf("%02d", ($timelist[5]-100)).sprintf("%02d", $timelist[2]).sprintf("%02d", $timelist[1]);
    my $TimeWSec      = sprintf("%02d", ($timelist[4]+1)).sprintf("%02d", $timelist[3]).sprintf("%02d", ($timelist[5]-100)).sprintf("%02d", $timelist[2]).sprintf("%02d", $timelist[1]).sprintf("%02d",$timelist[0]);
    my $ExtTime   = sprintf("%04d", ($timelist[5]+1900)).sprintf("%02d", ($timelist[4]+1)).sprintf("%02d", $timelist[3]).sprintf("%02d", $timelist[2]).sprintf("%02d", $timelist[1]).sprintf("%02d", $timelist[0]);
   
    my $conf_fh;  # *test+1 9/11 206168
    open($conf_fh, "<","$confFile") or die("readconfig: unable to open $confFile");
    my @elms = split("/", $INPUT_FILE);
    my $input_file = $elms[$#elms];
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Most unix shells would expand SSTEST*.txt on the command line to a list of  file names matching that pattern, unless no matching file names are found.

And if more than one matching file names are found, it does not appear that the  for ($i=0; ($i<@ARGV+0);) { loop would process them sensibly, though it is hard to be sure, since it also seems to have missing close }
0
 

Author Comment

by:jamar49
Comment Utility
Ozo I understand that and  I should have said in the being that script will work as long as I use the following :

perl splitfiles.pl -i SSTEST.5 digits.9 digits.YYYYMMDD.835 -c test.conf -e error.txt -r report.txt

But I was trying to use a wildcard because there will be multiple files and each file will have a different "5 digits.9 digits.YYYYMMDD" and I want automate script to run in cron.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I see where the for loop would set $INPUT_FILE to "SSTEST.5" but I don't see what would be done with digits.9 digits.YYYYMMDD.835

What would different "5 digits.9 digits.YYYYMMDD" look like?
0
 

Author Comment

by:jamar49
Comment Utility
file pattern  "SSTEST.5 digits.9 digits.YYYYMMDD.835"  examples are
SSTEST.12345.123456789.20131210.835 or SSTEST.12345.12345m789.20131210.835 or
 SSTEST.12345.000056789.20131210.835 and so fore.

The actual command would look like this:
perl splitfiles.pl -i SSTEST.12345.000056789.20131210.835 -c test.conf -e error.txt -r report.txt
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
Comment Utility
You might use
perl splitfiles.pl -i SSTEST.[0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].835 -c test.conf -e error.txt -r report.txt
Assuming there was one and only one file name matching that pattern.
Or, if you know you will always invoke the program with the same pattern, and you are able to modify the perl source code, you might build it in as a default parameter, and then you could also decide on the appropriate action in the case when more than one file name matches.
0
 

Author Comment

by:jamar49
Comment Utility
There will never be more than one file the exact same name
After running  perl splitfiles.pl -i SSTEST.[0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].835 -c test.conf -e error.txt -r report.txt

This is the error I get:
file] [-i <Input file>] [-e <error file>]                                  <
sh: Input: Cannot find or open the file.
$ o <orphaned claims>] [-d <detailed report>                                 <
sh: Syntax error: `newline or ;' is not expected.
$ y payor>] [-n 1] [-sc <allow check type sw                                 <
sh: report: Cannot find or open the file.
$ itch>]
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
The error you get seems to wrap at strange places, and also looks a lot like sh was trying to run the Usage message as a command.
Did you perchance try to put the perl command line inside of `` or $()?

And was there one and only one file name matching the pattern?
0
 

Author Comment

by:jamar49
Comment Utility
Did you perchance try to put the perl command line inside of `` or $()? No

And was there one and only one file name matching the pattern? No
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 125 total points
Comment Utility
The reason it didn't find anything with the wildcard was because the wildcard was looking for *.txt and the files don't have a .txt

If you take your original script and just remove the .txt after the * it should find the files.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
Comment Utility
The posted code only appears to parse one argument after the -i, so if more than one filename matches the pattern, it is not likely to work.
You would need to either use a pattern that matches exactly one file on the command line,
or modify the code to do something sensible when there are more than one file names after the -i or no file names after the -i.
You would also have to decide what that sensible action should be.
0
 

Author Comment

by:jamar49
Comment Utility
Give me an example of modify the code to do something sensible when there are more than one file names after the -i  that matches the pattern or no file names after the -i.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Give me an example of a sensible thing to do.

I would have thought that generating an error would be a sensible response to erroneous input.
0
 

Author Comment

by:jamar49
Comment Utility
New at perl but I need my code to work when more one filename matches the file pattern.
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 125 total points
Comment Utility
Wrap it in a shell loop:
for file in SSTEST*.txt; do perl splitfiles.pl -i $file -c test.conf -e error.txt -r report.txt; done

Or you can do the globbing in the Perl code; in this case, you need to quote the "SSTEST*.txt" argument in the shell:
@files = glob $filepattern;
for my $file (@files) { ... }
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now