Delete text in remote file through perl script

Below is the Perl Script which searches for a pattern in a remote file and the deletes 6 lines after that pattern.

I  want to delete the pattern also. how can i achieve this.

use File::Remote;
    use Symbol;
    my $remote = new File::Remote;
    my $fh     = Symbol::gensym;
    my $file   = "192.168.197.20:/home/file";
    $remote->open($fh,  $file) or die $!;
    my @lines = ();
    while (my $line = <$fh>) {
        push @lines, $line;
        if ($line =~ /zone "abhi.com" {/) {
            $line = <$fh> for 1 .. 6;    # read and ignore next 6 lines
        }
    }
    $remote->close($fh);
    $remote->open($fh, ">$file") or die $!;
    foreach my $line (@lines) {
        print $fh $line;
    }
    $remote->close($fh);
LVL 3
pehalwaanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
This should do it...
use File::Remote;
    use Symbol;
    my $remote = new File::Remote;
    my $fh     = Symbol::gensym;
    my $file   = "192.168.197.20:/home/file";
    $remote->open($fh,  $file) or die $!;
    my @lines = ();
    while (my $line = <$fh>) {
        if ($line =~ /zone "abhi.com" {/) {
            $line = <$fh> for 1 .. 6;    # read and ignore next 6 lines
        } else {
            # only save the line if it does not match the pattern
            push @lines, $line;
        }
    }
    $remote->close($fh);
    $remote->open($fh, ">$file") or die $!;
    foreach my $line (@lines) {
        print $fh $line;
    }
    $remote->close($fh);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pehalwaanAuthor Commented:
No, it doesn't work.
Cam't we replace the the text after searching in the file. i tried doing so but it throwed error. below is the Script which i tried.
use File::Remote;
    use Symbol;
    my $remote = new File::Remote;
    my $fh     = Symbol::gensym;
    my $file   = "192.168.197.20:/home/file";
    $remote->open($fh,  $file) or die $!;
    my @lines = ();
    while (my $line = <$fh>) {
        push @lines, $line;
        if ($line =~ /zone "abhi.com" {/) {
            $line = <$fh> for 1 .. 6;    # read and ignore next 6 lines
        }
    }
    $remote->close($fh);
$remote->open($fh,  $file) or die $!;
    while (my $line = <$fh>) {
        push @lines, $line;
        if ($line =~ /zone "abhi.com" {/) {
            s/$line//;
        }
    }
    $remote->close($fh);
    $remote->open($fh, ">$file") or die $!;
    foreach my $line (@lines) {
        print $fh $line;
    }
    $remote->close($fh);
0
wilcoxonCommented:
You could do it in a secondary loop but that will be much less efficient as it has to loop over the lines again.

What doesn't work with the code I provided?  The only change was only pushing onto @lines if the pattern does not match which should result in exactly the same output except skipping the line with the matching pattern (which is what you asked for).

Given the below input and output, everything appears to work correctly:
# input
zone "abhi.com" {
line 1
line 2
line 3
line 4
line 5
}
zone "kept.com" {
line 1b
line 2b
line 3b
line 4b
line 5b
}

Open in new window

# output
zone "kept.com" {
line 1b
line 2b
line 3b
line 4b
line 5b
}

Open in new window

0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

pehalwaanAuthor Commented:
oK. That works.
But another one-
One of my other script creates a new file(say abc) on the local system and adds some text to it.
I want that this script should also create this abc file on my remote server.
Please suggest.
0
wilcoxonCommented:
Pretty much the same as line 17-21:

$remote->open($fh, ">abc") or die $!;
# write stuff to the file
$remote->close($fh);
0
pehalwaanAuthor Commented:
my $file2="/etc/postfix/$domain";

                unlink("192.168.197.20:$file2") or die "Could not delete the file!\n";
Above Code is not deleting the remote file.
Is there something wrong in code?                
0
wilcoxonCommented:
Yes, you need to use the $remote object to call unlink (otherwise, you are calling the built-in which only works on local files):

$remote->unlink("192.168.197.20:$file2") or die "Could not delete the file\n";
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.