Shiel
Shiel

Reputation: 159

How can I extract and save text using Perl?

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;

Upvotes: 1

Views: 2785

Answers (5)

RET
RET

Reputation: 9188

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.

Upvotes: 2

Berserk
Berserk

Reputation: 433

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".

Upvotes: 1

benPearce
benPearce

Reputation: 38333

while (<IN>) {
  if (m/^(EX|ex)\d.*/) {   
    print OUT "$_";      
    print $_;   
  }
}

Upvotes: 3

raldi
raldi

Reputation: 22160

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.

Upvotes: 5

moritz
moritz

Reputation: 12842

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.

Upvotes: 1

Related Questions