In this article, we are going to discuss, how to search for single or multiple strings in a file and get all the matched lines along with their line numbers.

Check if a string exists in a file

To check if a given string exists in the file or not, we have created a function,

def check_if_string_in_file(file_name, string_to_search):
    """ Check if any line in the file contains given string """
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            if string_to_search in line:
                return True
    return False

It accepts a file path and a string as arguments. Then iterates over each line in the file one by one and for each line check if it contains the given string or not. If the line contains the given string, then return True. Whereas if no line in the file contains the given string, then it returns False.

Contents of the file ‘sample.txt’ are,

Hello this is a sample file
It contains sample text
Dummy Line A
Dummy Line B
Dummy Line C
This is the end of file

Let’s check if this file contains a string ‘is’ or not,
# Check if string 'is' is found in file 'sample.txt'
if check_if_string_in_file('sample.txt', 'is'):
    print('Yes, string found in file')
else:
    print('String not found in file')

Output:
Yes, string found in file

As file contains the ‘is’, therefore function check_if_string_in_file() returns True.

Here we get to know that file contains the given string or not. But what if we want to know all the exact occurrences of a string in the file like lines and line numbers. Let’s see how to do that,

Search for a string in file & get all lines containing the string along with line numbers

we have created a function, to get all the lines and line numbers which contain the given string,

def search_string_in_file(file_name, string_to_search):
    """Search for the given string in file and return lines containing that string,
    along with line numbers"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            line_number += 1
            if string_to_search in line:
                # If yes, then add the line number & line as a tuple in the list
                list_of_results.append((line_number, line.rstrip()))

    # Return list of tuples containing line numbers and lines where string is found
    return list_of_results

It accepts a file path and a string as arguments. In the end, it returns a list of tuples, where each tuple contains the line number and line, which includes the given string.

How did it worked ?

Algorithm is as follows,

  • Accept arguments – file path and a string to lookup.
  • Create an empty list of tuples.
  • Open the file at the given path in read-only mode.
  • Iterates over each line in the file one by one.
    • For each line, check if it contains the given string or not.
      • If the line contains the given string,
        • Creates a tuple of line number & the line and adds that to a list of tuples.
      • Return the list of tuples i.e., matched lines along with line numbers.

Suppose we have a file ‘sample.txt’ and its contents are,

Hello this is a sample file
It contains sample text
Dummy Line A
Dummy Line B
Dummy Line C
This is the end of file

Let’s get all the line along with line numbers which contain the word ‘is’,
matched_lines = search_string_in_file('sample.txt', 'is')

print('Total Matched lines : ', len(matched_lines))
for elem in matched_lines:
    print('Line Number = ', elem[0], ' :: Line = ', elem[1])

Output:
Total Matched lines :  2
Line Number =  1  :: Line =  Hello this is a sample file
Line Number =  6  :: Line =  This is the end of file

In total, there were two lines, which include the string ‘is’ and this function returned those lines along with their line numbers. Now suppose instead of search for a single string, we want to search for multiple strings in a file. Let’s see how to do that,

Search for multiple strings in a file and get lines containing string along with line numbers

To search for multiple strings in a file, we can not use the above-created function because that will open and close the file for each string. Therefore, we have created a separate function, that will open a file once and then search for the lines in the file that contains any of the given string i.e.

def search_multiple_strings_in_file(file_name, list_of_strings):
    """Get line from the file along with line numbers, which contains any string from the list"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            line_number += 1
            # For each line, check if line contains any string from the list of strings
            for string_to_search in list_of_strings:
                if string_to_search in line:
                    # If any string is found in line, then append that line along with line number in list
                    list_of_results.append((string_to_search, line_number, line.rstrip()))

    # Return list of tuples containing matched string, line numbers and lines where string is found
    return list_of_results

Let’s use this function,

Contents of the file ‘sample.txt’ are,

Hello this is a sample file
It contains sample text
Dummy Line A
Dummy Line B
Dummy Line C
This is the end of file

Let’s get all the lines along with their line numbers which either contain the word ‘is’ or ‘what’,
# search for given strings in the file 'sample.txt'
matched_lines = search_multiple_strings_in_file('sample.txt', ['is', 'what'])

print('Total Matched lines : ', len(matched_lines))
for elem in matched_lines:
    print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2])

Output:
Total Matched lines :  2
Word =  is  :: Line Number =  1  :: Line =  Hello this is a sample file
Word =  is  :: Line Number =  6  :: Line =  This is the end of file

The complete example is as follows,
def check_if_string_in_file(file_name, string_to_search):
    """ Check if any line in the file contains given string """
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            if string_to_search in line:
                return True
    return False


def search_string_in_file(file_name, string_to_search):
    """Search for the given string in file and return lines containing that string,
    along with line numbers"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            line_number += 1
            if string_to_search in line:
                # If yes, then add the line number & line as a tuple in the list
                list_of_results.append((line_number, line.rstrip()))

    # Return list of tuples containing line numbers and lines where string is found
    return list_of_results


def search_multiple_strings_in_file(file_name, list_of_strings):
    """Get line from the file along with line numbers, which contains any string from the list"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            line_number += 1
            # For each line, check if line contains any string from the list of strings
            for string_to_search in list_of_strings:
                if string_to_search in line:
                    # If any string is found in line, then append that line along with line number in list
                    list_of_results.append((string_to_search, line_number, line.rstrip()))

    # Return list of tuples containing matched string, line numbers and lines where string is found
    return list_of_results


def main():

    print('*** Check if a string exists in a file *** ')

    # Check if string 'is' is found in file 'sample.txt'
    if check_if_string_in_file('sample.txt', 'is'):
        print('Yes, string found in file')
    else:
        print('String not found in file')

    print('*** Search for a string in file & get all lines containing the string along with line numbers ****')

    matched_lines = search_string_in_file('sample.txt', 'is')

    print('Total Matched lines : ', len(matched_lines))
    for elem in matched_lines:
        print('Line Number = ', elem[0], ' :: Line = ', elem[1])

    print('*** Search for multiple strings in a file and get lines containing string along with line numbers ***')

    # search for given strings in the file 'sample.txt'
    matched_lines = search_multiple_strings_in_file('sample.txt', ['is', 'what'])

    print('Total Matched lines : ', len(matched_lines))
    for elem in matched_lines:
        print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2])

if __name__ == '__main__':
    main()

Output:
*** Check if a string exists in a file *** 
Yes, string found in file
*** Search for a string in file & get all lines containing the string along with line numbers ****
Total Matched lines :  2
Line Number =  1  :: Line =  Hello this is a sample file
Line Number =  6  :: Line =  This is the end of file
*** Search for a multiple string in a file and get lines containing string along with line numbers ***
Total Matched lines :  2
Word =  is  :: Line Number =  1  :: Line =  Hello this is a sample file
Word =  is  :: Line Number =  6  :: Line =  This is the end of file

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.