Value for the second or third octet that is equivalent to the octet value entered by the user

I’m trying to figure out how to take a octet input value and print IP addresses in a file that have a value for the second or third octet that matches the input value. I set up a code already that prompts the user to enter a value and compares the value to the entirety of the file, printing out each line the input value is included in. The problem is that I need to print the ones where the input value is only a second or third octet. Here is my code at the moment.

# opening and reading the file 
file_read = open('Applog.txt', "r")

#Prompt the user for an IP address octet value
text = input("Enter the IP address octet value: ")

# read the file line by line.
lines = file_read.readlines()

new_list = []
idx = 0

# loop through each line in the file
for line in lines:
    # If the line has a input string, take the index of that line and place it into a new list
    if text in line:
        new_list.insert(idx, line)
        idx += 1
file_read.close()

# If the length of new list is 0, the input string isn't found in the text file
if len(new_list)==0:
    print("\n\"" +text+ "\" is not found in \"" +'Applog.txt'+ "\"!")
else:
    # displays the lines containing given octet value
    lineLength = len(new_list)
    print("\n**** Lines containing \"" +text+ "\" ****\n")
    for i in range(lineLength):
        print(end=new_list[i])
    print()

The text file is filled with lines like

[node1] - 190.223.252.106 - User Successful Login
[node2] - 239.84.157.20 - User Successful Profile Picture Upload
[node2] - 87.130.185.37 - User Successful Login
[node6] - 210.155.211.219 - User Successful Payment
[node5] - 64.103.198.103 - User Successful Login
[node2] - 136.4.60.67 - User Successful Login
[node7] - 191.161.59.79 - User Failed Login
[node1] - 128.215.207.129 - User Failed Payment
[node6] - 203.141.97.180 - User Successful Profile Picture Upload
[node6] - 172.218.65.224 - User Successful Profile Picture Upload

I hope someone can find out what I’m missing!

In order to do that you need to parse the lines to extract the IP
addresses, and then parse those to extract the octets. As it is, your
test reads:

 if text in line:

which just looks for a substring anywhere in the line. Suppose the
user entered 6. That would match, 190.223.252.106 but also,
excitingly, [node6].

It looks like your fields are separated by the string " - ". You could
go:

 fields = line.split(' - ')

and look at fields[1]. You can use an unpacking assignmenthere:

 node_part, ip_part, comment = line.split(' - ', 2)

Then you want to break the ip_part apart on dots:

 ip_parts = ip_part.split('.')

and then test ip_parts[1] and ip_parts[2] against the value in
text.

Cheers,
Cameron Simpson cs@cskk.id.au

I tried your solution but it didn’t seem to change anything. The code didn’t break or anything, it just stayed the same as the previous take. Did I do this right?

for line in lines:
    # If the line has a input string, take the index of that line and place it into a new list
    if text in line:
        new_list.insert(idx, line)
        idx += 1
        fields = line.split('-')
        node_part, ip_part, comment = line.split(' - ', 2)
        ip_parts = ip_part.split('.')
file_read.close()

Not really. Let’s look at the if-statement:

 if text in line:
     new_list.insert(idx, line)
     idx += 1
     fields = line.split('-')
     node_part, ip_part, comment = line.split(' - ', 2)
     ip_parts = ip_part.split('.')

Your test for the octet is unchanged. It still tests text in line.
You seem to have missing the part in my message which said:

and then test ip_parts[1] and ip_parts[2] against the value in
text.

All your stuff with fields and ip_parts etc happens after you’ve
decided if the line matches. You want to make that decision after
you’ve got ip_parts, and base that decision on the apprpriate test of
those parts instead of using the overly broad text in line test.

So all the parsing to get ip_parts needs to be before the
if-statement, and the if-statement needs to test the desired bits of
ip_parts, not the whole line.

Cheers,
Cameron Simpson cs@cskk.id.au

Thank you for your help, but this is giving one more issue. I figured out where to put the code but now the input is giving me second/third octets with the numbers 0 and 1 instead of just 100. Here is the full code to recap.

# opening and reading the file 
file_read = open('Applog.txt', "r")

#Prompt the user for an IP address octet value
text = input("Enter the IP address octet value: ")

# read the file line by line.
lines = file_read.readlines()

new_list = []
idx = 0

# loop through each line in the file
for line in lines:
    fields = line.split('-')
    node_part, ip_part, comment = line.split(' - ', 2)
    ip_parts = ip_part.split('.')
    # If the line has a input string, take the index of that line and place it into a new list
    if ip_parts[1] in text:
        new_list.insert(idx, line)
        idx += 1
    if ip_parts[2] in text:
        new_list.insert(idx, line)
        idx += 1
file_read.close()

# If the length of new list is 0, the input string isn't found in the text file
if len(new_list)==0:
    print("\n\"" +text+ "\" is not found in \"" +'Applog.txt'+ "\"!")
else:
    # displays the lines containing given octet value
    lineLength = len(new_list)
    print("\n**** Lines containing \"" +text+ "\" ****\n")
    for i in range(lineLength):
        print(end=new_list[i])
    print()

Now that you have parsed the IP address you use == not in.

Becomes

    If ip_parts[1] == text:

fields = line.split(‘-’)

You’re not using this line, it can be discarded.

node_part, ip_part, comment = line.split(’ - ‘, 2)
ip_parts = ip_part.split(’.')

If the line has a input string, take the index of that line and place it into a new list

if ip_parts[1] in text:

Barry has pointed out that because the text should be an exact match for
the particular ip_part you’re testing, you want == and not in. The
in operator with strings is a substring test.

   new_list.insert(idx, line)
   idx += 1

if ip_parts[2] in text:
new_list.insert(idx, line)
idx += 1

The other problem is that if the text matches both the second and
third octets you’re adding the line twice, once for each match. You want
an or test:

 if ip_parts[1] == text or ip_parts[2] == text:

This tests both octets, and is true if one or the other or both match.
Then you’d have just one if-statement and add the line just once.

Cheers,
Cameron Simpson cs@cskk.id.au

Thank you everyone, you have fixed my problem