perlsubstitution

Perl: inline variable substitution not working


Below is the perl script

my $var="
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
da:be:66:35:0e:47:0f:60:f1:70:a4:43:ff:39:da:65.
Please contact your system administrator.
Add correct host key in /home/PCS/.ssh/known_hosts to get rid of this message.
Offending key in /home/PCS/.ssh/known_hosts:144
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
Filesystem                 1K-blocks       Used  Available Use% Mounted on
/dev/mapper/vg00-lscratch 7288301568 6079377352 1208924216  84% /scratch
";

$var =~ s/.+Filesystem/Filesystem/g;
print "$var\n";

What it is printing is ($var value):

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
da:be:66:35:0e:47:0f:60:f1:70:a4:43:ff:39:da:65.
Please contact your system administrator.
Add correct host key in /home/PCS/.ssh/known_hosts to get rid of this message.
Offending key in /home/PCS/.ssh/known_hosts:144
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
Filesystem22                 1K-blocks       Used  Available Use% Mounted on
/dev/mapper/vg00-lscratch 7288301568 6079377352 1208924216  84% /scratch

It seems substitution is not working.

As per my knowledge, $var should have print the below, but it is not. How can it be done? What's wrong in this substitution?

Filesystem                 1K-blocks       Used  Available Use% Mounted on
/dev/mapper/vg00-lscratch 7288301568 6079377352 1208924216  84% /scratch

Solution

  • . matches any character other than a LF. But you want to match any character incl LF. You use /s to do that.

    s/.*Filesystem/Filesystem/sg
    

    Clarify that Filesystem appears at the start of a line (to avoid problems with paths that contain Filesystem):

    s/.*^Filesystem/Filesystem/msg
    

    Prevent needless backtracking on failed match:

    s/\A.*^Filesystem/Filesystem/msg
    

    Remove repetition:

    s/\A.*^(?=Filesystem)//msg