Solved

how to grep these two IP address

Posted on 2014-04-15
6
1,308 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now