0

I am trying to print the lines having specific strings only from multiple strings. I tried below code using enumerate and its work, but i want to check for multiple string to find out the lines.

Sample Data

ctive fint-upload-report "remotehost"\n', 'interface ethernet 0\n', ' ip address 10.64.81.5 255.255.255.240\n', ' no trapenable\n', ' exit\n', 'ip address 10.64.81.5 255.255.255.240\n', 'ip domainname localdomain\n', 'ipv6 domainname localdomain\n', 'ntp server 172.16.0.10\n', 'timezone Asia Calcutta\n', '! Iptables Configuration\n', '!PTP Configuration\n', 'ConnectPtpVersion 1\n', 'ConnectPtp  3    "1/3/1"     norm     mib   7     51380481     3  -1    303\n', 'ConnectPtp  3    "1/3/2"     norm     direct   7     50331904     10  -1    301\n', '!PTP End\n', '\n', 'rlnode radioLinkPtpNodeObjects 2\n', 'rlnode boardParameters 1 3 1 \n', '! CF configuration\n', 'config_file_name BKP_42106\n', '! command group end\n', '! SU configuration\n', 'su-activerelease 2\n', 'su-release CXP9010021_3 MINI-LINK_TN5.4FP3.2_LH1.6FP3.2_R32T102 1\n', 'su-release CXP9010021_1 MINI-LINK_TN_5.4FP.4_LH_1.6FP.4_R33C133 2\n', 'su-sbl_type 1 1\n', 'su-sbl_type 2 1\n', 'su-loadmodule CXCR102059_1 R11T01 1\n', 'su-loadmodule CXCR102051_1 R11T01 1\n', 'su-loadmodule CXCR102004_1 R6N03 1\n', 'su-loadmodule CXCR102050_1 R1G02 1\n', 'su-loadmodule CXP9011133_1 R16M13 1\n', 'su-loadmodule CXP9011133_2 R16M13 1\n', 'su-loadmodule CXP9011133_3 R16M13 1\n', 'su-loadmodule CXP9011133_4 R16M13 1\n', 'su-loadmodule CXP9011133_5 R16M13 1\n', 'su-loadmodule CXP9011133_6 R16M13 1\n', 'su-loadmodule CXP9011133_7 R7M19 1\n', 'su-loadmodule CXP9011133_9 R7M19 1\n', 'su-loadmodule CXP9011133_20 R7M19 1\n', 'su-loadmodule CXP9012118_1 R16M13 1\n', 'su-loadmodule CXC1725538_1 R1G03 1\n', 'su-loadmodule CXC1732476_1 R4M13 1\n', 'su-loadmodule CXC1731531_1 R2M02 1\n', 'su-loadmodule CXP9021456_1 R4A03 1\n', 'su-loadmodule CXP9014938_104 R32T102 1\n', 'su-loadmodule CXP9014938_105 R32T102 1\n', 'su-loadmodule CXP9012516_102 R32T102 1\n', 'su-loadmodule CXP9012516
import pandas
file = open ('2020_08_18_0000_NE.23152.GDNP11_1.10_64_81_5.0.txt').readlines()
Myindex = [l for l, elem in enumerate (file) if 'su-release' in elem]  
for ind in Myindex:
    line = file[ind]
    print(line)

Output:

su-release CXP9010021_3 MINI-LINK_TN5.4FP3.2_LH1.6FP3.2_R32T102 1

su-release CXP9010021_1 MINI-LINK_TN_5.4FP.4_LH_1.6FP.4_R33C133 2
0

1 Answer 1

1

Here is one approach:

  • open the file with context manager
  • enumerate with start=1 (since we are counting line numbers)
  • for each line: map() each test_string to a boolean value expressing of whether exists in the line or not
  • then use any() to aggregate the boolean results (think of it as or operator in this case)

Here is sample code:

#!/usr/bin/env python3

tests_strings = [
    'su-activerelease',
    'su-release'
]

with open('2020_08_18_0000_NE.23152.GDNP11_1.10_64_81_5.0.txt') as file:
    my_index = filter(
        lambda n_line: any(map(lambda test_string: test_string in n_line[1], tests_strings)),
        enumerate(file.readlines(), start=1)
    )

    for line_number, line in my_index:
        print(line_number, '-', line)
Sign up to request clarification or add additional context in comments.

3 Comments

The code i have posted is also working, but i want to add list of strings in if statement , without using or. As i have to add 15-20 string.
@SunilSharma Your question is ambiguous. You mean you want to check against multiple strings?
@SunilSharma see the updated answer and I've added an explanation how it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.