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?
 
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
 
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.