LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2024, 12:12 PM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 936

Rep: Reputation: 44
[BASH] grep literally the whole word only and nothing more?


From the following list, I want to remove all IP addresses that have the same value as subnets.


Code:
cat input.out
192.168.1.208
192.168.1.208/32
192.168.1.213/32
192.168.1.214/32
192.168.1.253/32
192.168.1.254
192.168.1.254/28
192.168.1.245/32
192.168.1.180
192.168.1.181
For that I am using "w" flag in grep to match the whole word.
That however, is also matching addresses with subnet.

Code:
cat input.out | grep -Evw "192.168.1.208|192.168.1.254"
192.168.1.213/32
192.168.1.214/32
192.168.1.253/32
192.168.1.245/32
192.168.1.180
192.168.1.181
Question:
How can i filter out IP addresses that have the same value as subnets and keep the ones with subnet?
This is what I am trying to get.

Code:
192.168.1.208/32
192.168.1.213/32
192.168.1.214/32
192.168.1.253/32
192.168.1.254/28
192.168.1.245/32
192.168.1.180
192.168.1.181
 
Old 10-02-2024, 12:29 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,778

Rep: Reputation: 2659Reputation: 2659Reputation: 2659Reputation: 2659Reputation: 2659Reputation: 2659Reputation: 2659Reputation: 2659Reputation: 2659Reputation: 2659Reputation: 2659

Read the start of "grep --help" (and/or the relevant part of man grep) more carefully.


Last edited by boughtonp; 10-02-2024 at 12:30 PM.
 
Old 10-02-2024, 12:43 PM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 936

Original Poster
Rep: Reputation: 44
cat input.out | grep -Evx "192.168.1.208|192.168.1.254"

Thanks!
 
Old 10-02-2024, 12:57 PM   #4
lvm_
Senior Member
 
Registered: Jul 2020
Posts: 1,203

Rep: Reputation: 403Reputation: 403Reputation: 403Reputation: 403Reputation: 403
/ is a word delimiter so -w won't help but "192\.168\.1\.1$" will work. And it's not so much the knowledge of grep that is required but familiarity with regular expressions - google it.

Quote:
Originally Posted by czezz View Post
How can i filter out IP addresses that have the same value as subnets and keep the ones with subnet?
But for this task grep us not sufficient, you'll need something more powerful e.g. gawk.
 
Old 10-02-2024, 06:36 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,956

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
-x (full line match) is ok.
In regular expression a dot means "any character", so a precise match is
Code:
grep -Evx "192\.168\.1\.208|192\.168\.1\.254" input.out
or
Code:
grep -Fvx -e "192.168.1.208" -e "192.168.1.254" input.out
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to grep on grep with regex? How to match only numbers and dots from a grep? debianfella Linux - Software 13 02-06-2024 02:59 PM
regex for phrase like'word-word-word' Zero4 Linux - General 9 07-06-2019 07:36 AM
grep exact word - to find file which start with some word publicLearner Linux - Newbie 8 07-23-2015 04:12 AM
finding a word by using grep in whole file system c2431993 Linux - Newbie 2 09-29-2010 07:38 PM
Literally know nothing about ethernet zella_19 Linux - Newbie 1 03-14-2002 12:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 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