Print/Return all the lines with matching sentence using re

PHOTO EMBED

Wed Jan 11 2023 02:30:26 GMT+0000 (Coordinated Universal Time)

Saved by @praveenms079 ##python ##re ##match ##sentence

import re

def getAllLinesWithMatchingSentence(filePath,sentence):
	"""
	Prints all the lines that has sentence in it
    
    filePath : str
    	path of a file to search
        worked for .txt , .log files
    
    sentence: str
    	string to search
    """
    
    with open(filePath,'r') as f:
        for line in f:
            match = re.search(r"{}".format(sentence),line)

            if match:
                print(line)

 # Following function prints all the lines in a filePath that matches given sentence
getAllLinesWithMatchingSentence(
    filePath= 'files/log1.log',
    sentence = "dataframe of shape")
content_copyCOPY