Am using the following grep to match a particular pattern in grep.

grep xyz abc.txt

now while i run this command, if the pattern matched, am getting the line containing xyz


xyz is doing some work

Now if I want the file name also along with my output, what should I do


abc.txt:xyz is doing some work

I need this kind of output in Solaris. gnu had a option of -H in grep. But I need for Solaris flavour.

migrated from stackoverflow.com Mar 21 '13 at 17:56

This question came from our site for professional and enthusiast programmers.


If you don't have -H switch, use instead :

awk '/pattern/{print FILENAME":"$0}'
  • Fixed typo in my response – Gilles Quenot Mar 21 '13 at 12:58
  • that awk line cannot replace the grep one. but works for the example in question. – Kent Mar 21 '13 at 12:59
  • Thx, this worked. – user2195153 Mar 21 '13 at 13:09

The filename is printed before each match when you search multiple files:

grep xyz abc.txt /dev/null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy