Search code examples
unixgrep

Display filename before matching line


How can I get grep to display the filename before the matching lines in its output?


Solution

  • Try this little trick to coax grep into thinking it is dealing with multiple files, so that it displays the filename:

    grep 'pattern' file /dev/null
    

    To also get the line number:

    grep -n 'pattern' file /dev/null