Question

I have a text file with the following lines as example

This is line
This is line with test
This is ine with 2test
This is line 2
This is line 3
This is line with 3test
This is line 4
This is line with 4test

Now I want a code to change the text file as follows:

Lines with test
This is line with test
This is ine with 2test
This is line with 3test
This is line with 4test

Lines without test
This is line
This is line 2
This is line 3
This is line 4

I am using the following code. I am assuming my code would print the title with every line but I am not able to execute the code due to some errors. Can you please help me?

#!/usr/bin/perl
use strict;
use warnings;
open(FH, '<filetest.txt');
my @queues = <FH>;
close(FH);
open(OFH,'>testfile.txt');
my $name;
foreach $name(@queues)
{
if($name =~ 'test')
{
print OFH "Lines with test\n";
print OFH $1;
}
else{
print OFH "Lines without test\n";
print OFH $1;
}
close(OFH);
}

Note: I corrected the error to remove the syntax errors but still there is nothing being written to the file testfile.txt

Was it helpful?

Solution

Have a try with:

#!/usr/bin/perl
use strict;
use warnings;

my $infile = 'filetest.txt';
my $outfile = 'testfile.txt';

# use 3-arg open and if open succeeded
open my $fh_in, '<', $infile or die "Unable to open file '$infile' for reading: $!";

my @with_test;
my @without_test;
while (<$fh_in>) {
    if (/test/) {
        push @with_test, $_;
    } else {
        push @without_test, $_;
    }
}
close $fh_in;

open my $fh_out, '>', $outfile or die "Unable to open file '$outile' for writting: $!";
print $fh_out "Lines with test\n";
print $fh_out @with_test;
print $fh_out "Lines without test\n";
print $fh_out @without_test;

close $fh_out;

OTHER TIPS

I haven't tested this. The idea is to write the "with test" lines to the file immediately The "without test" lines are stored (in an array called "without") until the end of the program and then written

#!/usr/bin/perl
use strict;
use warnings;
open(FH, '<filetest.txt');
my @queues = <FH>;
close(FH);
open(OFH,'>testfile.txt');
my $name;
my @without=();
foreach $name(@queues)
{
if($name =~ 'test')
{
  print OFH $name;
}
else{
  push @without, $name;
}
print OFH "\n\nlines without\n";
print OFH @without;

}

also you should add a local $/ before reading the file handle. check this out: how to read the whole file. the match format should be like this: if($somestr =~ /#/). and you shouldn't close the filehandle in the loop.

#!/usr/bin/perl

use strict;
use warnings;
local $/;
open(FH, 'file#.txt');
my $s;
$s = <FH>;
close(FH);
open(FH, '>file#.txt');
my @queues = split(/\n/, $s);
my @arr;
my @arr2;
for($s=0; $s<$#queues+1; $s++)
{

    if($queues[$s] =~ /#/)
    {
        push(@arr, $queues[$s]."\n");
    }
    else
    {
        push(@arr2, $queues[$s]."\n");
    }
}
print FH "lines with #\n";
print FH @arr;
print FH "lines without #\n";
print FH @arr2;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top