views:

1544

answers:

7

No extracted data output to data2.txt? What goes wrong to the code?

MyFile.txt

ex1,fx2,xx1
mm1,nn2,gg3
EX1,hh2,ff7

This is my desired output in data2.txt:

ex1,fx2,xx1
EX1,hh2,ff7


#! /DATA/PLUG/pvelasco/Softwares/PERLINUX/bin/perl -w

my $infile  ='My1.txt';
my $outfile ='data2.txt';

open IN,  '<', $infile  or die "Cant open $infile:$!";
open OUT, '>', $outfile or die "Cant open $outfile:$!";

while (<IN>) {   
  if (m/EX$HF|ex$HF/) {
    print OUT $_, "\n";      
    print $_;   
  }
}

close IN;
close OUT;
+1  A: 

When I run your code, but name the input file My1.txt instead of MyFile.txt I get the desired output - except with empty lines, which you can remove by removing the , "\n" from the print statement.

moritz
Oh sorry I forgot to edit My1.txt. It should be MyFile.txt.
Shiel
+4  A: 

This regex makes no sense:

m/EX$HF|ex$HF/

Is $HF supposed to be a variable? What are you trying to match?

Also, the second line in every Perl script you write should be:

use strict;

It will make Perl catch such mistakes and tell you about them, rather than silently ignoring them.

raldi
... and the third should be `use warnings`.
Brad Gilbert
He already has -w on the first line.
raldi
Well why doesn't he just add -Mstrict to the first line?
Brad Gilbert
+3  A: 
while (<IN>) {
  if (m/^(EX|ex)\d.*/) {   
    print OUT "$_";      
    print $_;   
  }
}
benPearce
Also, if you don't need the (debug?) output of all lines in the input file, you can reduce this to the one-liner perl -ne 'print if /^(EX|ex)\d/'
Jouni K. Seppänen
perl golf has its place, but I'd rather people put readable code into production.
John Ferguson
This *is* simple enough to use a one-liner.
Brad Gilbert
+2  A: 

Bleh! "use strict;" "use warnings;". Lexical-filehandles. Three-args-open.

Shlomi Fish
A: 
kanja
+1  A: 

The filenames don't match.

open(my $inhandle, '<', $infile)   or die "Cant open $infile: $!";
open(my $outhandle, '>', $outfile) or die "Cant open $outfile: $!";

while(my $line = <$inhandle>) {   

    # Assumes that ex, Ex, eX, EX all are valid first characters
    if($line =~ m{^ex}i) {         # or   if(lc(substr $line, 0 => 2) eq 'ex') {
        print { $outhandle } $line;      
        print $line;
    }
}

And yes, always always use strict;

You could also chomp $line and (if using perl 5.10) say $line instead of print "$line\n".

Berserk
What are the braces for in this line? print { $outhandle } $line;
raldi
It helps avoid mistakes like... print $outhandle, $line; (the comma means print won't recognise $outhandle as a file handle). Its a recommendation from "Perl Best Practises" by Damian Conway.
draegtun
I didn't realize that would work.
Brad Gilbert
+2  A: 

Sorry if this seems like stating the bleeding obvious, but what's wrong with

grep -i ^ex < My1.txt > data2.txt

... or if you really want to do it in perl (and there's nothing wrong with that):

perl -ne '/^ex/i && print' < My1.txt > data2.txt

This assumes the purpose of the request is to find lines that start with EX, with case-insensitivity.

RET