• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2389
  • Last Modified:

how to grep these two IP address

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
NYGiantsFan
Asked:
NYGiantsFan
1 Solution
 
sentnerCommented:
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
 
Dan CraciunIT ConsultantCommented:
Is grep different? I thought the escape character is \

1\.1\.1\.1
0
 
MazdajaiCommented:
grep -iP '2\.2.\2\.2|1\.1\.1\.1'  *

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
simon3270Commented:
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
 
NYGiantsFanAuthor Commented:
It worked without the -w
0
 
simon3270Commented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now