sampson has asked for the wisdom of the Perl Monks concerning the following question:
by running#!/usr/bin/perl use strict; use warnings; use Regexp::Common qw(net); use String::Util qw(trim); while (<>) { /$RE{net}{IPv4}{-keep}/ and printf +"%s\n", trim($1); /$RE{net}{IPv6}{-sep => ':'}{-style => 'HeX'}{-keep}/ and printf +"%s\n", trim($1); /$RE{net}{MAC}{-keep}/ and printf +"%s\n", trim($1); }
Then I used vi's dd to remove lines and got this file (remove real IPs and MAC address before posting here)cat report.txt | ./ips.pl > ips.txt
When I further filter with GNU coreutil's uniq, I got different results from "uniq" Vs "uniq -u". I read online that this is mainly due to spaces in the data file. When I print in the perl script, I trim() already. What might went wrong in the above script?192.168.1.188 fdba:7b43:1916::d34 fe80::ec78:4ff:fec0:a17b 192.168.1.1 fe80::eade:27ff:feb6:fa8c 192.168.1.1 fdba:7b43:1916::1 fdba:7b43:1916::d34 fdba:7b43:1916::d34 fdba:7b43:1916::d34 fdba:7b43:1916::d34 fdba:7b43:1916::d34
cat perlout.txt | sort | uniq 192.168.1.1 192.168.1.188 fdba:7b43:1916::1 fdba:7b43:1916::d34 fe80::eade:27ff:feb6:fa8c fe80::ec78:4ff:fec0:a17b cat perlout.txt | sort | uniq -u 192.168.1.188 fdba:7b43:1916::1 fe80::eade:27ff:feb6:fa8c fe80::ec78:4ff:fec0:a17b When pre-filter one more time with uniq, then they give same result cat perlout.txt | sort | uniq | uniq 192.168.1.1 192.168.1.188 fdba:7b43:1916::1 fdba:7b43:1916::d34 fe80::eade:27ff:feb6:fa8c fe80::ec78:4ff:fec0:a17b cat perlout.txt | sort | uniq | uniq -u 192.168.1.1 192.168.1.188 fdba:7b43:1916::1 fdba:7b43:1916::d34 fe80::eade:27ff:feb6:fa8c fe80::ec78:4ff:fec0:a17b
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl output and uniq
by Fletch (Bishop) on May 01, 2020 at 19:30 UTC | |
by sampson (Initiate) on May 02, 2020 at 12:06 UTC | |
|
Re: Perl output and uniq
by jo37 (Curate) on May 01, 2020 at 20:04 UTC | |
by sampson (Initiate) on May 02, 2020 at 12:07 UTC | |
|
Re: Perl output and uniq
by afoken (Chancellor) on May 02, 2020 at 20:49 UTC |