'Returns the lines of the specified file that match the ``pattern``.
This keyword reads a file from the file system using the defined
``path``, ``encoding`` and ``encoding_errors`` similarly as `Get File`.
A difference is that only the lines that match the given ``pattern`` are
returned. Lines are returned as a single string catenated back together
with newlines and the number of matched lines is automatically logged.
Possible trailing newline is never returned.
A line matches if it contains the ``pattern`` anywhere in it and
it *does not need to match the pattern fully*. The pattern
matching syntax is explained in `introduction`, and in this
case matching is case-sensitive.
Examples:
| ${errors} = | Grep File | /var/log/myapp.log | ERROR |
| ${ret} = | Grep File | ${CURDIR}/file.txt | [Ww]ildc??d ex*ple |
If more complex pattern matching is needed, it is possible to use
`Get File` in combination with String library keywords like `Get
Lines Matching Regexp`.
``encoding_errors`` argument is new in Robot Framework 2.8.5.'

def grep_file(self, path, pattern, encoding='UTF-8', encoding_errors='strict'):

    pattern = ('*%s*' % pattern)
    path = self._absnorm(path)
    lines = []
    total_lines = 0
    self._link("Reading    file    '%s'.", path)
    with io.open(path, encoding=encoding, errors=encoding_errors) as f:
        for line in f.readlines():
            total_lines += 1
            line = line.rstrip('\r\n')
            if fnmatch.fnmatchcase(line, pattern):
                lines.append(line)
        self._info(('%d    out    of    %d    lines    matched' % (len(lines), total_lines)))
        return '\n'.join(lines)
