user3819295 user3819295 - 3 months ago 20
Perl Question

Convert shell command to pure Perl



Please help me to find a way to convert this shell command to pure Perl

ssh $IP1 netstat -Aan | awk '{print $5" "$6}' | grep 2049 | egrep -v "$IP1|$IP2" | wc -l


I want to avoid starting a separate shell process just to execute this command.

This is how the output of the first part of the command should look

ssh $IP1 netstat -Aan | awk '{print $5" "$6}'

10.11.100.26.443 10.12.81.57.1985
10.25.107.26.443 10.84.85.60.1967
10.55.137.86.443 10.34.82.20.1947


I tried several ways and failed

@found = grep { /2049/ && !/$IP/ } `$SSH $FacingIP[0] netstat -Aan`;


I just need to add the
awk
part.

Answer Source

If I understand your question correctly, it could be better written as follows:

How does one extract the fifth and sixth whitespace-separated field from a list of lines.

For example,

my @lines = (
    "a b c d 10.11.100.26.443 10.12.81.57.1985\n",
    "a b c d 10.25.107.26.443 10.84.85.60.1967\n",
    "a b c d 10.55.137.86.443 10.34.82.20.1947\n",
);

should produce

[ '10.11.100.26.443', '10.12.81.57.1985' ],
[ '10.25.107.26.443', '10.84.85.60.1967' ],
[ '10.55.137.86.443', '10.34.82.20.1947' ],

I'd use

map { [ (split)[4,5] ] } @lines