Print only matched pattern in perl

Hi,

I have script like below:

#!/usr/local/bin/perl

use strict;
use warnings;

while (<DATA>) {

  ( my ($s_id) = /^\d+\|(\d+?)\|/ ) ;

  if ( $s_id == 1 ){

    s/^(.*\|)*.*ABC\.pi=([\d.]+|[\w.]+)*.*ABC\.id=(\d+|[\w.]+).*$/$1$2|$3/s;

    print "$1$2|$3\n";

    }
  }


__DATA__
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.66~ABC.id=789137136770
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.67~ABC.id=789134713670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.68~ABC.id=789137213670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.69~ABC.id=78913713670
123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id=78913713670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id=78913713670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id=789137135670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id=789137153670
123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713670
123|1|456464|645646|4546|654~abc~dhghga~121322~456466874~8796896
123|2|456464|645646|4546|654~abc~dhghga~121322~456466874~6788708
123|2|456464|645646|4546|654~abc~dhghga~121322~456466874~6806

When I am executing I am getting output as follows:

123|1|456464|645646|4546|112.33.44.55.66|789137136770
123|1|456464|645646|4546|112.33.44.55.67|789134713670
123|1|456464|645646|4546|112.33.44.55.68|789137213670
123|1|456464|645646|4546|112.33.44.55.69|78913713670
Use of uninitialized value $2 in concatenation (.) or string at split_test.pl line 14, <DATA> line 5.
Use of uninitialized value $3 in concatenation (.) or string at split_test.pl line 14, <DATA> line 5.
1|
123|1|456464|645646|4546|112.33.44.55.70|78913713670
123|1|456464|645646|4546|112.33.44.55.70|78913713670
123|1|456464|645646|4546|112.33.44.55.70|789137135670
123|1|456464|645646|4546|112.33.44.55.70|789137153670

I am looking to get rid off the error. How can I do it?

try the below

 
print "$1$2|$3\n";

to
 
if ($1 && $2 && $3) {print "$1$2|$3\n";}

You are getting that error because, on line 5

123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713670

there is no matching pattern. itkamaraj gave a good solution. May be you could parse only lines with the pattern ABC.??