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.

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

filename_where_keyword_appears_01.txt
filename_where_keyword_appears_02.txt
etc

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

2 Answers 2

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

 
discard

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.