LinuxQuestions.org
Help answer threads with 0 replies.
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 05-29-2024, 07:22 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 931

Rep: Reputation: 44
[BASH] grep IP address and IP with network mask in one go?


Initial file:
Code:
cat file.txt 
172.16.100.0/24
148.180.112.67
Following grep will extract IP address only from file.txt
Code:
cat file.txt | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
172.16.100.0
148.180.112.67
Following grep will extract IP with its netmask only (will skip any IP addresses without \netmask)
Code:
cat file.txt | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\/[0-9][0-9]'
172.16.100.0/24
I was trying to combine both adding -E flag but that wont work:
Code:
cat file.txt | grep -o -E "'[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'|'[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\/[0-9][0-9]'"
Ofc, i can run it in 2 steps and then process/merge outputs.
However, i was wondering if anyone knows some clever way to have it all in one go?

Last edited by czezz; 05-30-2024 at 04:42 AM.
 
Old 05-29-2024, 07:28 AM   #2
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 931

Original Poster
Rep: Reputation: 44
Ah, i think i sorted it out. I was close
Code:
cat file.txt | grep  -E ''[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'|'[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\/[0-9][0-9]''
 
1 members found this post helpful.
Old 05-29-2024, 07:37 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,707

Rep: Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627

Code:
grep -oP '\b[\d.]{7,15}(/\d{1,2})?\b' file.txt
Then validate the output, accordingly.


Last edited by boughtonp; 05-29-2024 at 07:52 AM.
 
1 members found this post helpful.
Old 05-29-2024, 07:41 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,636

Rep: Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525
Do you mean this?
Code:
grep -E 'regex1|regex2' file.txt
https://unix.stackexchange.com/quest...-regex-problem
 
Old 05-29-2024, 07:54 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,236

Rep: Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150
Ah, I love the resurrection of an old chestnut like parsing IP addresses.

Nuthin' new in the (*nix) universe ...
 
Old 05-30-2024, 11:12 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,915

Rep: Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236
Code:
grep -Eo '[0-9]{1,3}(\.[0-9]{1,3}){3}(/[0-9]{1,2})?' file.txt
With bash-builtins:
Code:
while read x; do [[ $x =~ [0-9]{1,3}(\.[0-9]{1,3}){3}(/[0-9]{1,2})? ]] && echo "$BASH_REMATCH"; done < file.txt

Last edited by MadeInGermany; 05-30-2024 at 11:15 AM.
 
1 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 01:59 PM
ip , network address , subnet mask basics sumeet inani Linux - Newbie 3 05-11-2014 11:33 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
network Address subnet mask kapilbajpai88 Linux - Newbie 4 04-28-2009 04:27 PM
Setting IP Address and Subnet Mask and whatnot... Sloanr333 Linux - Networking 6 11-06-2003 11:39 PM

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

All times are GMT -5. The time now is 01:51 AM.

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