Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Problem with perl

I using Debian Linux 3.0 and Perl 5.6.1
I have this text file (the directory name : /home/student) that i want to insert the
keywords into the database.
 Yield Report
Job      : PQFP64    Start Date: 05/31/04
Tray     : Peak-14   Start Time: 14:40:35
Package  : PQFP-64   Sensor Ht : 239.17mils
Operator : CINDY     Run Time  : 16:11:53
Mach S/N : LSI318    Lot       : D4550027
There's a table with 4 columns  in the text file.      

Perl Program
The below program enables me to extract keywords from multiple text file  
and insert the current directory of the files.


use DBI;
use File::Spec;
$directory = File::Spec->curdir();
print "$directorycld\n";

@ARGV = <*.txt>;
my ( $job, $tray, $date, $lot);
my( $dbh, $sth, $query );

$dbh = DBI->connect( 'dbi:mysql:rvsi');
$query = 'insert into test ( job, date, tray, lot, dir ) values ( ?, ?, ?, ? , ?)';
$sth = $dbh->prepare( $query );

while(<>) {
  if( /Job\s*:\s*(\S+)\s*Start Date\s*:\s*(\S+)/ ) {
    $job = $1;
    $date = $2;
    print "$job\n";
    print "$date\n";
  } elsif( /Tray\s*:\s*(\S+)/ ) {
    $tray = $1;
    print "$tray\n";
  } elsif( /Lot\s*:\s*(\S+)/ ) {
    $lot = $1;
    print "$lot\n";
  if( ( $job ne '' ) && ( $date ne '' ) && ( $tray ne '' ) && ( $lot ne '' ) ) {
    # We have a full record

    $sth->execute($job, $date, $tray, $lot, $directory);

    $job = $date = $tray = $lot = '';



The output when the perl program is executed (database):

| no | job    | tray    | lot      | date     | dir  |
| 36 | PQFP65 | Peak-15 | D4550028 | 06/31/04 | .    |
| 37 | PQFP65 | Peak-15 | D4550028 | 07/25/04 | .    |

currently, i face this 2 problems:

1> the program only enable me to insert multiple text files with the filename ending
 with .txt but not all my filename have .txt (eg abc.txt and cde).
2> it is unable to insert the current directory path of the text files into the dir
table of the database (as shown in the output above)

how can i modify my perl program to enable this two function??
  • 5
  • 3
  • 2
2 Solutions
1> you can add additional strings to the globbing operator you're using. Something like
  @ARGV = <*.txt *.cde>;

2> to better see what the File::Spec::curdir method has given you, you should change the line reading

  print "$directorycld\n";

to read

  print "$directory\n";


Now, as I read the doc, 'curdir' returns "." on UNIX and Linux. And this matches what you are seeing. To get the full pathname, you need to call rel2abs. So change the lines

use File::Spec;
$directory = File::Spec->curdir();

to be

use File::Spec::Functions qw(curdir rel2abs);
$directory = rel2abs( curdir());

TPolyAuthor Commented:
thanks it works..

however is it possible to include the name of the file in the directory?
And it still could not read files without .txt

Pls help
thank you.
1> What are the specific files you wanted to read that it is not reading? What does your current glob expression look like?

2> OK, I've been misled by the names given to things. When you call something $directory, I think you mean you want the directory name. It sounds like what you really want is the file's absolute pathname.

It's a trick of the way that the <> operator works that the name of the current file is in the $ARGV variable.

Inside the 'if' statement before you do your $sth->execute, say:

my $fname = "$directory/$ARGV";


my $fname = rel2abs( $ARGV);

then use $fname instead of $directory in the parameter list. I'll leave it up to you to change the column name.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

TPolyAuthor Commented:
thanks it works great for the directory path!!!
sorry, but this perl script is still unable to extract
data from those files without the .txt
1> What are the specific files you wanted to read that it is not reading? What does your current glob expression look like?
TPolyAuthor Commented:
for example i have all this files (all the format of the files are the same):

test1, test2, test3.txt, test4.txt.

the above perl program only allows me to extract the data from test3.txt and test4.txt
i need to extract the data from all 4 files.

btw, what is current glob expression? how to find it?

@ARGV = <test*>;
TPolyAuthor Commented:
oh ok i get what you mean

but my actual filenames will be like
DF758  KL133  AS122.txt DF098

so do i need to edit @ARGV = <test*>; to something else?

Are there files in the directory you don't want to process?
If not, you might use
  @ARGV = <*>;
otherwise, is there any way to distinguish the files you want from the files you don't want?
TPolyAuthor Commented:
thanks guys~ :)    
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now