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,

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,

Let’s check if this file contains a string ‘is’ or not,

Output:

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,

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,

Let’s get all the line along with line numbers which contain the word ‘is’,

Output:

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.

Let’s use this function,

Contents of the file ‘sample.txt’ are,

Let’s get all the lines along with their line numbers which either contain the word ‘is’ or ‘what’,

Output:

The complete example is as follows,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.