Skip to main content
added 238 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

With GNU grep or any grep with perl-like regexp support, you can use the negative look-ahead operator:

grep -P '\b(?!192\.168\.)(\d{1,3})(\.(?1)){3}\b' 

Or directly with perl:

perl -ne 'print if /\b(?!192\.168\.)(\d{1,3})(\.(?1)){3}\b/' 

Those report the lines that contain a quad-decimal representation of an IP address other than those starting with 192.168., even if those lines also contain a 192.168.x.y IP address.

For a stricter matching of IP addresses, you could use the Regexp::Common::net module:

perl -MRegexp::Common=net -ne ' print if m{ \b$RE{net}{IPv4}{-keep}\b (?(?{$2 == 192 && $3 == 168}) (*FAIL)) }x' 

With GNU grep or any grep with perl-like regexp support, you can use the negative look-ahead operator:

grep -P '\b(?!192\.168\.)(\d{1,3})(\.(?1)){3}\b' 

Or directly with perl:

perl -ne 'print if /\b(?!192\.168\.)(\d{1,3})(\.(?1)){3}\b/' 

Those report the lines that contain a quad-decimal representation of an IP address other than those starting with 192.168., even if those lines also contain a 192.168.x.y IP address.

With GNU grep or any grep with perl-like regexp support, you can use the negative look-ahead operator:

grep -P '\b(?!192\.168\.)(\d{1,3})(\.(?1)){3}\b' 

Or directly with perl:

perl -ne 'print if /\b(?!192\.168\.)(\d{1,3})(\.(?1)){3}\b/' 

Those report the lines that contain a quad-decimal representation of an IP address other than those starting with 192.168., even if those lines also contain a 192.168.x.y IP address.

For a stricter matching of IP addresses, you could use the Regexp::Common::net module:

perl -MRegexp::Common=net -ne ' print if m{ \b$RE{net}{IPv4}{-keep}\b (?(?{$2 == 192 && $3 == 168}) (*FAIL)) }x' 
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

With GNU grep or any grep with perl-like regexp support, you can use the negative look-ahead operator:

grep -P '\b(?!192\.168\.)(\d{1,3})(\.(?1)){3}\b' 

Or directly with perl:

perl -ne 'print if /\b(?!192\.168\.)(\d{1,3})(\.(?1)){3}\b/' 

Those report the lines that contain a quad-decimal representation of an IP address other than those starting with 192.168., even if those lines also contain a 192.168.x.y IP address.