Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

what I'm looking to do is boost the command I already have to return line numbers of where the specified string appears:

grep -r -l -n -w "ePKI" /home/websites/www/* > /home/keywords.txt

The command above returns filenames like


And what I would like it to do is return the line number of where the string appears in file too, like:

filename_where_keyword_appears_01.txt 12 45 67 89

Could anyone possibly advise me on improving my command to do something like this?

share|improve this question

You might get similar results to what you want by omitting -l and using cut as follows:

grep -r -n -w "ePKI" /home/websites/www/*|cut -d':' -f 1,2 > /home/keywords.txt

If you need the output to be exactly as you described, you could try with awk:

awk -F, 'FNR==1 {printf "\n%s",FILENAME};/ePKI/ {printf " %d ",NR} END {printf "\n"}' /home/websites/www/*

Combine it with find for recursion:

find /home/websites/www/* -type f -print0 | xargs -0 awk -F, 'FNR==1 {printf "\n%s",FILENAME};/ePKI/ {printf " %d ",NR} END {printf "\n"}'
share|improve this answer

Option -l suppresses normal output and prints only the file name. Omit option -l and keep option -n to see the file name and the line numbers. Additionally you will see the matching line, or only the matching part when specifying option -o.

share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.