Solved

how to grep these two IP address

Posted on 2014-04-15
6
1,527 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
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.:

770 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