LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-22-2015, 08:46 PM   #1
upthehill
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Rep: Reputation: Disabled
regex question cant exclude number range


ip add show| awk '!/'inet6\|link\|valid\|BROADCAST\|lo:\|\(127\|172\|10.0.[2-4]\)'/','/inet / {ip = $2; print ip}'| cut -d / -f1
returns

10.0.1.5

but I want to exclude 172.15-31 but I can’t get it to ?

What am I doing wrong ?



[peterpicked@mylaptop ~]$ ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:14:8e:03:64:38 brd ff:ff:ff:ff:ff:ff
inet6 fe80::218:8bff:fe03:6438/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:0a:cd:1c:ed:18 brd ff:ff:ff:ff:ff:ff
4: eth0.10@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 00:14:8e:03:64:38 brd ff:ff:ff:ff:ff:ff
inet 10.0.1.5/24 brd 10.0.1.255 scope global eth0.10
inet6 fe80::218:8bff:fe03:6438/64 scope link
valid_lft forever preferred_lft forever
5: eth0.20@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 00:14:8e:03:64:38 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.5/24 brd 10.0.2.255 scope global eth0.20
inet6 fe80::218:8bff:fe03:6438/64 scope link
valid_lft forever preferred_lft forever
6: eth0.30@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 00:14:8e:03:64:38 brd ff:ff:ff:ff:ff:ff
inet 10.0.3.5/24 brd 10.0.3.255 scope global eth0.30
inet6 fe80::218:8bff:fe03:6438/64 scope link
valid_lft forever preferred_lft forever
7: eth0.40@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 00:14:8e:03:64:38 brd ff:ff:ff:ff:ff:ff
inet 10.0.4.5/24 brd 10.0.4.255 scope global eth0.40
inet6 fe80::218:8bff:fe03:6438/64 scope link
valid_lft forever preferred_lft forever
8: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
link/ether e2:c9:6b:e2:e5:91 brd ff:ff:ff:ff:ff:ff
inet 172.17.42.1/16 scope global docker0
inet6 fe80::e0c9:6bff:fee2:e591/64 scope link
valid_lft forever preferred_lft forever
[peterpicked@mylaptop ~]$
 
Old 01-23-2015, 07:58 AM   #2
sudowtf
Member
 
Registered: Nov 2013
Posts: 205

Rep: Reputation: 46
it works for me with your data. do you mean you want to "include"? if so remove "\|172" from your command.
 
Old 01-24-2015, 09:22 AM   #3
upthehill
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
if for example a 172.1.x.x was listed I want it to display but if a 172.15.x.x through to 172.31.x.x was found it would exclude.

I thought I could do

ip add show| awk '!/'inet6\|link\|valid\|BROADCAST\|lo:\|\(127\|172.[15-31]\|10.0.[2-4]\)'/','/inet / {ip = $2; print ip}'| cut -d / -f1

but its not working.
 
Old 01-25-2015, 11:41 AM   #4
RegexMaster
LQ Newbie
 
Registered: Jan 2015
Distribution: Xubuntu, FreeBSD
Posts: 1

Rep: Reputation: Disabled
First you need to generate regex for numeric range. For example with my web app you can get it for 15-31 . The result is
Quote:
Code:
/(?:(?:(?:1[5-9])|(?:2[0-9])|(?:3[0-1])))/
.
Then add it with grep at the end. With your example it will be something like
Quote:
Code:
ip add show| awk '!/'inet6\|link\|valid\|BROADCAST\|lo:\|\(127\|172\|10.0.[2-4]\)'/','/inet / {ip = $2; print ip}'| cut -d / -f1 | grep -vP '^172\.(?:(?:(?:1[5-9])|(?:2[0-9])|(?:3[0-1])))'
The idea is to use "invert matching" after you get those ip's.

Last edited by RegexMaster; 01-25-2015 at 11:42 AM.
 
  


Reply

Tags
regex



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] using awk to parse csv and exclude field range rylincoln Linux - Software 3 09-12-2011 11:26 PM
[SOLVED] 'find' command regex to exclude hidden files? arashi256 Linux - Newbie 3 06-28-2010 08:53 AM
[SOLVED] exclude ip range Imranteli Linux - Newbie 2 08-20-2009 10:31 PM
How can i exclude an ip address from given range in dhcp server? saurabh_pandya4 Linux - Networking 4 03-05-2009 03:39 AM
Regex to exclude a specific phrase grob115 General 5 11-19-2008 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration