Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx help and file concatenate 2

Status
Not open for further replies.

PerlIsGood

Programmer
Jan 24, 2002
154
US
Been a while since I worked with Perl. Can someone help me out with this. I'm reading a directory of comma delimited files. I need to read in all files, add a few variables based on each file's name, and write out another file containing all files' info.

Here's what I have so far, but can't get the regex working properly...

The regular expression I'm attempting to build needs to capture this type of filename:

^(everything) - (everything) - (####.##.##)

Also -- each file has it's own header, how do i read each file starting from the second line?

ALl suggestions welcome. Thanks!

Code:
$dir = "E:\\dir\\";
opendir DIR, $dir or die "Could not open $dir directory: $!";
@totalfiles = readdir DIR;
closedir DIR;

# header row
$outfile = 'item,region,date,other headers...\n';

#data
foreach ( sort @totalfiles ) {
	next if ($_ =~ m/^\./);
	m/^(.+?) - (.+?) - (\d4.\d2.\d2) \d+?.txt$/;
	my($item) = $1;
	my($region) = $2;
	my($date) = $3;
	open FILE, "$dir\\$_" or die "Could not open file $_: $!";
		while ( chomp (@file = <FILE>) ) {
			
			$outfile += $item . "," . $region . "," . $date . "," . shift @file;
		}
	close FILE;
}

$f = "E:\\dir\\file.txt";
open(OUTP, ">$f");
 print OUTP "$outfile";
close(OUTP);

Notorious P.I.G.
 
Okay I was able to hack some more into this. the file is being created, all I need to know is how to point to the second row of each file, and how to get the regex working...

Code:
open(OUTP, "> $f");
print OUTP $outfile;

#data
foreach ( sort @totalfiles ) {
	next if ($_ =~ m/^\./);

        #REGEX not returninng anything
	m/(.+?) - (.+?) - (\d4\.\d2\.\d2)/;
	my($item) = $1;
	my($region) = $2;
	my($date) = $3;

	open FILE, "$dir/$_" or die "Could not open file $_: $!";
	while ( $file = <FILE> ) {
		$text = "$item,$region,$date," . $file;
		print OUTP $text; # . @file;
		}
	close FILE;
}

close OUTP;

Notorious P.I.G.
 
to 'skip' the first line:

Code:
    [url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] FILE, [red]"[/red][purple][blue]$dir[/blue]/[blue]$_[/blue][/purple][red]"[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Could not open file [blue]$_[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
    [url=http://perldoc.perl.org/functions/readline.html][black][b]readline[/b][/black][/url][red]([/red]FILE[red])[/red][red];[/red]
    [olive][b]while[/b][/olive] [red]([/red] [blue]$file[/blue] = <FILE> [red])[/red] [red]{[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Add the 'x' modifier to your regex so that random spacing will be ignored. Then simply enforce that the regex must match, otherwise of course the placeholders will be empty.

Code:
#data
foreach ( sort @totalfiles ) {
    [COLOR=green]next unless[/color] m/(.+?) - (.+?) - (\d4\.\d2\.\d2)/[COLOR=green]x[/color];
    my ($item, $region, $date) = ($1, $2, $3);
 
looks suspicious:

(\d4\.\d2\.\d2)

should be:

(\d{4}\.\d{2}\.\d{2})

que no?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Doh! That was a pretty stupid mistake.. Guess that's what I get for slacking for so many years...

Thanks for the help works perfectly now!

Notorious P.I.G.
 
de nada notorioso [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top