Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from 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}'
share|improve this answer
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 way to thanks here is to upvote/accept answers =) – Gilles Quenot Mar 21 '13 at 14:51

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

grep xyz abc.txt /dev/null
share|improve this answer

You must log in to answer this question.