Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Output:

xyz is doing some work

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

Expected

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 stackoverflow.com Mar 21 '13 at 17:56

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

2 Answers 2

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

awk '/pattern/{print FILENAME":"$0}'
share|improve this answer
    
Fixed typo in my response –  sputnick 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 =) –  sputnick 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.