Arun
Arun

Reputation: 669

File editing in perl

Hi I am trying to delete a content of file based on regex match. Here is the following code:

my $file = "Cioin_PatchAnalysis.txt";
local $/ = 'Query=';
my @content = ();
open (INFILE, $file) || die "error2: $!";
while (<INFILE>) 
    {
    chomp;
    if ($_ =~ /\s*3374_Cioin/) 
     {#capture the query sequence
        @content = $_;
        print @content;
     }
    }

Sample data is:

===================================================================
Query= 3374_Cioin
         (24,267 letters)

Database: /home/aprasanna/BLAST/DMel_renamedfile.fasta 
           14,047 sequences; 7,593,731 total letters

Reference: Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schaffer, 
Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), 
"Gapped BLAST and PSI-BLAST: a new generation of protein database search
programs",  Nucleic Acids Res. 25:3389-3402.

Query= 578_Antlo
         (88 letters)
=========================================================

I wish to remove from Query =3374_Coin... till -3402. i.e till next record separator. I am able to store the matched part in @content. However, I am not able to delete it in the original file. I wish my original file only has Query= 578_Antlo!

I am very new to Perl.

Upvotes: 0

Views: 74

Answers (1)

Srgrn
Srgrn

Reputation: 1825

The easiest way is to simply write all lines you do want into some other file.

I would suggest something like:

my $file = "Cioin_PatchAnalysis.txt";
my $outfile = "Fixed_Cioin_PatchAnalysis.txt";
local $/ = 'Query=';
my @content = ();
open (INFILE, $file) || die "error2: $!";
open(my $outfile, '>', $outfile) or die "Could not open file '$outfile' $!";
while (<INFILE>) 
    {
    chomp;
    if ($_ !~ /\s*3374_Cioin/) 
     {#capture the query sequence
        @content = $_;
        print $outfile @content;
     }
    }

Than you can replace the original with the new file. Another option is to keep all the lines that doesn't match the regex, than print them back into the original file:

my $file = "Cioin_PatchAnalysis.txt";
local $/ = 'Query=';
my @content = ();
open (INFILE, $file) || die "error2: $!";

while (<INFILE>) 
    {
    chomp;
    if ($_ !~ /\s*3374_Cioin/) 
     {#capture the query sequence
        push @content, $_;
     }
    }

open(my $outfile, '>', $file) or die "Could not open file '$outfile' $!";
print $outfile @content;

Upvotes: 1

Related Questions