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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-29-2024, 07:22 AM
|
#1
|
Member
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 931
Rep:
|
[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.
|
|
|
05-29-2024, 07:28 AM
|
#2
|
Member
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 931
Original Poster
Rep:
|
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.
|
05-29-2024, 07:37 AM
|
#3
|
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,707
|
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.
|
05-29-2024, 07:54 AM
|
#5
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,236
|
Ah, I love the resurrection of an old chestnut like parsing IP addresses.
Nuthin' new in the (*nix) universe ...
|
|
|
05-30-2024, 11:12 AM
|
#6
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,915
|
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.
|
All times are GMT -5. The time now is 01:51 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|