The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 07-16-2008
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 271
Perl regex question

I have the following code:

Code:
#!/usr/bin/perl -w

@files = <*.csv>;
foreach $file (@files) {
  open(FH, $file) || die("Error: Cannot open file $file for reading.");
  my @dt = ($file =~ /^(\w+).(\d{6})\.csv$/);
  while (<FH>) { 
    print "@dt[0] $_\n";
  }
  close(FH);
}

There is redundancy in this code as it first checks for all files ending in ".csv" (line 3) and subsequently parses the filename (line 6) looking for characters and digits. How do I change line 3 into a regular expression, such that line 6 can be removed and the array @dt be determined there?