Link to home
Start Free TrialLog in
Avatar of pehalwaan
pehalwaanFlag for Afghanistan

asked on

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);
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pehalwaan

ASKER

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);
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

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.
Pretty much the same as line 17-21:

$remote->open($fh, ">abc") or die $!;
# write stuff to the file
$remote->close($fh);
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?                
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial