Using Grep to Find IPs in a txt file

I am trying to figure out how to use the grep regex command to filter out IPs.
My first problem is to tell how many IPs only have one digit in the first octet.
And the second is to find all IPs with the number 244 in the first octet.

Ive tried grep -o -n '\b[0-9].' file.txt

but this just finds any 1 digit, then I also tried setting up each place [0-9]{1,3} but I still dont get the righ output.

file format:

192.168.1.1
54.28.144.21
1.58.65.44
244.98.55.11

output 1:

1.58.65.44

output 2:

244.98.55.11

Any help would be much appreciated

In an RE (regular expression) the dot means "any character"; you certainly want \. or [.] which means a literal dot.
Even this is not precise enough; the \b boundary can match a preceding dot, so it is a single-digit somewhere (but at the very end - a dot must follow).
With this input file you can use the ^ to mark the beginning of the line.
Then, the -o option returns only the matched portion, not the full line. With the -o you must cover the entire IP address.

@Tiberious01 ,

  • have you searched online for 'grep for IP address'

  • is this for IPv4 / IPv6 addresses or both ?

  • use the markdown menu options to distinguish code/data/text (colleague @MadeInGermany has edited your initial post, you need to do so going forwards)

Yes I have looked up searching for IP addresses with grep. And yes there are only IPv4 addresses in the file.

I tried grep '\b[0-9]\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt and I get no output am I missing something?

what are you expecting from that ? (alway show expected results - sames time/avoids ambiguity etc)

given:

cat input 
192.168.1.1
54.28.144.21
1.58.65.44
58.1.65.44
65.58.1.44
244.98.55.11
77.244.77.77
77.77.244.77
77.77.77.244

grep -E '^[[:digit:]]{1}\.' input
1.58.65.44

grep '\b[0-9].[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' input 

grep -E '\b[0-9].[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' input 
192.168.1.1
1.58.65.44
244.98.55.11
77.244.77.77

Yes:
the {1,3} quantifier works in ERE.
But grep defaults to BRE where it is \{1,3\}
You can switch to ERE with
grep -E ... or
egrep ...

Another potential problem is \b (word boundary) that works in certain GNU/Linux versions only. Safer is \< (left word boundary, and \> is right word boundary).
However, most precise is ^ (the beginning of the line, and $ marks the end of the line).

Hi @Tiberious01,

taking the hints from @munkeHoller and @MadeInGermany, i will try to convey the solutions:

  1. How many IPs only have one digit in the first octet

First, you need the atom for beginning of line ^, which in a regex represents a null string and is a meta character. In addition, the character class for digits [:digit:] or a range 0-9 (the minus - works as a shorthand for 012..9), enclosed by brackets [], which represent a non-empty list of charachters. Inside brackets, any meta character looses its meta property.
Without further quantifiers, both bracket expressions [[:digit:]] and [0-9] match exactly one digit. But since we have to 'count' the digits of the first octet, we need in addition the octet separator, namely the dot .. But in a regex, it's another meta character, it stands for resp. represents any single character. To override its meta property, it must either be escaped by a \ or be enclosed by brackets (see above):

$ grep '^[0-9]\.' file.txt
# or
$ grep ^[0-9]\\. file.txt # w/o quotes, so double escaped due to shell's interpretation
# or
$ grep ^[[:digit:]][.] file.txt
# or
$ grep ^[0-9][.] file.txt

To count the results, we can use

$ grep ^[0-9][.] file.txt | wc -l
# or
$ grep -c ^[0-9][.] file.txt

See man wc and man grep.

  1. Find all IPs with the number 244 in the first octet

Here you have to look for a constant number resp. string resp. word, not just any digit.Then it becomes very simple:

$ grep ^244 file.txt

A separator isn't needed (why?).