UNIX Interview Questions

When you use a single file as input to the grep command to search for a pattern, it won’t print the filename in the output. Now write a grep command to print the filename in the output without using the ‘-H’ option.

grep pattern filename /dev/null
The /dev/null or null device is special file that discards the data written to it. So, the /dev/null is always an empty file.
Another way to print the filename is using the ‘-H’ option. The grep command for this is
grep -H pattern filename

Leave a Reply