<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def grep(pattern, *files_or_paths):
 """Returns lines matched in fnames, where fnames can either be pathnames to files to grep through or open file objects to grep through line by line"""

    matches = []

    for fop in files_or_paths:
        with fileobj(fop) as fo:
            matches.extend((line for line in fo if re.match(pattern, line)))

    return matches