user11392987
user11392987

Reputation:

How to grep specific IP Addresses using regex?

My simplified sample file is as follows ... The actual file has more text and IP in it. Just to make it easier for this example.

file.txt

10.1.1.9
10.1.1.33
10.1.1.35

I would like to grep only 10.1.1.9 & 10.1.1.33.

If I use grep '10.1.1.[9|33]' file.txt, this will grep everything including .35.

I know this can be achieve with grep -v 35 file.txt, but I wanted the solution in regex as the actual file contains more data than this sample.

What's wrong with my regex and how to fix it?

[user@linux]$ grep '10.1.1.[9|33]' file.txt
10.1.1.9
10.1.1.33
10.1.1.35
[user@linux]$

Desired Output (without .35)

[user@linux]$ grep '10.1.1.[regex here]' file.txt
10.1.1.9
10.1.1.33
[user@linux]$

Upvotes: 3

Views: 1475

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133528

It should be done with simple grep.

grep -E '10\.1\.1\.(9|33)'  Input_file

Where -E option is(from man grep):

   -E, --extended-regexp
          Interpret PATTERN as an extended regular expression (ERE, see below).  (-E is specified by POSIX.)

Upvotes: 3

Related Questions