Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to grep these two IP address

Posted on 2014-04-15
6
1,621 Views
Last Modified: 2014-04-18
Hi,

I have two IP addresses

2/.2/.2./2

or

1/.1/.1/.1

I want to grep for both

I tried

grep -E -o "2/.2./2./2|1/.1/.1/1"  *
grep -E "2/.2./2./2|1/.1/.1/1"  *

Without success (the above commands are not pulling the correct IP)

Any ideas? Thanks.
0
Comment
Question by:NYGiantsFan
6 Comments
 
LVL 14

Expert Comment

by:sentner
ID: 40002037
Why do you have forward slashes in there?  Replace those with backslashes (\) instead and you should be ok (presuming that the source file itself doesn't have slashes in there for some reason).
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40002040
Is grep different? I thought the escape character is \

1\.1\.1\.1
0
 
LVL 21

Expert Comment

by:Mazdajai
ID: 40002127
grep -iP '2\.2.\2\.2|1\.1\.1\.1'  *

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 40002333
The bare grep for 1.1.1.1 will also match 221.1.1.123, because it doesn't care what comes before the first "1" or after the last.  In know this is an artificial example, but it can still be a problem with real IP addresses - e.g. if you are looking for 192.168.0.10, the pattern '192\.168\.0\.10' will also match 192.168.0.102.

To get round that, add the "-w" option, so that grep will only match strings on word boundaries (e.g. the "\.1" at the end of the pattern is followed by a punctuation character, or a space, or the end of a line).

Yo also need the "-E" to use extended regular expressions (for the "|")

So, the test becomes

     grep -Ew '1\.1\.1\.1|2\.2\.2\.2' *
0
 

Author Closing Comment

by:NYGiantsFan
ID: 40004824
It worked without the -w
0
 
LVL 19

Expert Comment

by:simon3270
ID: 40009226
It works now, but it may fail in the future with different IP addresses and different data to be searched!

Thanks for the points!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question