I have website on my hard drive that I need to locate keywords within the HTML files, then output to an html file, listing each find on a new line. Currently i'm useing the line below, but the output is not ideal.

grep -lUirn  -C 1  "Word1" *  >> report.html

the above line produces something like:

      file1.html file2.html etc.html

but i need it to output to the file as:




even more ideal would be to output it as:




closed as unclear what you're asking by G-Man, Sparhawk, Mr Shunz, Anthony Geoghegan, telcoM Apr 28 at 8:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to U/L. I'm quite confused by your question. The -l flag outputs the name of the matching file, but it should output one filename per line. (Is that what you want?) I'm not sure what the -n option does here. Since you are only outputting filenames, I don't think it does anything. We also don't know the rules to derive the "relitivePath" from the original filenames. – Sparhawk Apr 20 at 7:09
  • "relitivePath" is not critical, whats critical is getting the output to individual lines instead of a continues line without CR's – David Apr 20 at 7:36
  • 1
    I just noticed you also have -C which also makes no sense with -l. I can't replicate this behaviour. I create the files with for i in {1..3}; do echo Word1 > file$i.html; done, then grep -lUirn -C 1 "Word1" puts each filename on a separate line. What version of grep are you using? – Sparhawk Apr 20 at 9:22

Using GNU grep, the -U option (treat all files as binary) has no effect unless you are running on MS-DOS or Windows, and -n (output line numbers of matching lines) has no effect when combined with -l (output only the names of the matching files). Likewise, -C 1 (a single line of context around the match) does not have any effect with -l.

The matched files are already reported on separate lines, so I don't quite see why you get them on a single line.

grep -lr -Fiw 'Word1' .

should do what you want. I added -F to do string comparisons instead of regular expression matches, and -w to do matching of complete words (not substrings).

To convert the output to URIs:

grep -lr -Fiw 'Word1' . | sed 's,^,http://,'

This just inserts http:// at the start of each line using sed.

All of this obviously assumes that no filename contains any embedded newlines. If they did, sed would insert its string before each newline-delimited part of the filename.

  • Your syntax adds the http:// nicely but i'm still getting everything concatenated together. How do i add CR after each line? – David Apr 20 at 19:03
  • @David What Unix are you running? grep -l should output one single pathname per line, always. I'm as confused as Sparhawk as to why your grep -l does not output one filename per line. – Kusalananda Apr 20 at 19:31
  • I'm using OS X at the moment – David Apr 20 at 19:33
  • @David Well, I tested on macOS Mojave, and grep -l definitely does not output multiple filenames per line. Do you have a single file called literally file1.html file2.html etc.html with spaces inbetween? – Kusalananda Apr 20 at 19:34
  • I found my error. In a text editor LF's show, but opening in browser did not. I spaced on adding <br>. This solved my issue. grep -lr -Fiw 'word' * | sed 's,^,<BR>http://,' >> Report.html Thanks for the help everyone! – David Apr 20 at 20:38

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