Link to home
Start Free TrialLog in
Avatar of jamar49
jamar49

asked on

How to correct Perl Input file error?

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.
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

"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?
Avatar of jamar49
jamar49

ASKER

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
Avatar of jamar49

ASKER

There isn't anything else that starts SSTEST that I don't want to split
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.
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jamar49

ASKER

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];
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 }
Avatar of jamar49

ASKER

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.
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?
Avatar of jamar49

ASKER

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
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.
Avatar of jamar49

ASKER

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>]
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?
Avatar of jamar49

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jamar49

ASKER

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.
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.
Avatar of jamar49

ASKER

New at perl but I need my code to work when more one filename matches the file pattern.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial