Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

replace a line when regex match...

by perlknight (Pilgrim)
on Apr 11, 2005 at 20:36 UTC ( [id://446762]=perlquestion: print w/replies, xml ) Need Help??

perlknight has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, I need to replace a line in a file when a regex is match. e.g:
host-abc:/var/log/messages:::read_intr;I/O error;AddrMarkNotFound;Data +Request Error:
when there is "host-abc" and "messages" in the line replace the whole line. Thanks.

Replies are listed 'Best First'.
Re: replace a line when regex match...
by revdiablo (Prior) on Apr 11, 2005 at 20:40 UTC

    What have you tried? The simplest solution is probably something along the lines of $_ = "something" if /foo/ and /bar/, but you'll get better responses if you show what did or didn't work in your particular case.

Re: replace a line when regex match...
by tlm (Prior) on Apr 11, 2005 at 20:44 UTC

    In general, this will do it:

    perl -i.bak -pe 's/before/after/' filename
    This changes your file in place, and puts a back up in filename.bak. The only thing left is specifying the s/// expression, but from your post I can't figure out exactly what you want. So, exactly, what should the lines to be replaced look like, and what should the replacement look like?

    the lowliest monk

Re: replace a line when regex match...
by lithron (Chaplain) on Apr 11, 2005 at 20:45 UTC
    A solution.
    $_ = "host-abc:/var/log/messages::::read_intr"; if (/host-abc.*messages/) { $_ = "new string"; }
    Is that what you're looking for?
      Yes. This is what I am looking for. This is what I have so far; what I am trying to do is log into several remote host and check for an existing entry, and change the whole line. If one does not exist then add it to the file.
      #!/usr/bin/perl -w use strict; use Net::SSH::Perl; my $rcmd = " perl -e 'use strict; open(FD, filetochange.txt) or die \"can n +ot open FD: $!\"; while (<FD>) { if (/mon/ && /messages/) { print $_; #change line here } } close(FD); '"; my $ssh = Net::SSH::Perl->new('localhost', protocol=>('2,1')); $ssh->login('joe1','blabla'); my($stdout, $stderr, $exit) = $ssh->cmd("$rcmd"); print $stdout;
      But I am getting this error:
      Use of uninitialized value in concatenation (.) or string at ./hash_ms +gstab.pl line 6
      Any idea? or does some one have a better idea of loging into a remote box and running an inline script on it? Thanks.
        does some one have a better idea of loging into a remote box and running an inline script on it?

        Inlining like this can be a real pain. In your case, the interpolation phase is clobbering all your variables before you even pass the code to $ssh->cmd. You should probably be using single quotes. I think it would be a lot easier to avoid the inlining altogether, though, and just save the script to a file on the remote host.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://446762]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-24 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found