Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem with perl

Posted on 2004-08-02
10
227 Views
Last Modified: 2010-03-05
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.

#!/usr/bin/perl

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 = '';

  }
}

$dbh->disconnect();

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??
thanks
0
Comment
Question by:TPoly
  • 5
  • 3
  • 2
10 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 11700565
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());




0
 

Author Comment

by:TPoly
ID: 11701003
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.
0
 
LVL 20

Accepted Solution

by:
jmcg earned 400 total points
ID: 11701192
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";

or

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.

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:TPoly
ID: 11701535
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
0
 
LVL 20

Expert Comment

by:jmcg
ID: 11701686
1> What are the specific files you wanted to read that it is not reading? What does your current glob expression look like?
0
 

Author Comment

by:TPoly
ID: 11701908
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?

thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 11701979
@ARGV = <test*>;
0
 

Author Comment

by:TPoly
ID: 11702039
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?

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 11702154
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?
0
 

Author Comment

by:TPoly
ID: 11711552
thanks guys~ :)    
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do I grep for a specfic DNS record in a directory 3 48
stftime format 4 56
Perl script not writing to .csv file 6 111
Move Function in Perl Script 2 72
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 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