1

Over the past couple of days I've been trying to dig through directories of files to report on files containing key words. Through the help of other users showing me the correct syntax for grep and sed, I’ve come up with the following solution which has worked well. Unfortunately, it’s a bit repetitive and I'd like to refactor it.

echo "<HR><BR><B><h2>Search for Keyword:  KeyWord1<BR></B></h2><ol>"  >> temp.txt
grep -lr -Fiw 'KeyWord1' * | sed -e 's|.*|<li><a href="http://&">&</a></li>|'  >>  temp.txt
echo "</ol>"   >> temp.txt 

echo "<HR><BR><B><h2>Search for Keyword:  KeyWord2<BR></B></h2><ol>"  >> temp.txt
grep -lr -Fiw 'KeyWord2' * | sed -e 's|.*|<li><a href="http://&">&</a></li>|'  >>  temp.txt
echo "</ol>"   >> temp.txt 

echo "<HR><BR><B><h2>Search for Keyword:  KeyWord3<BR></B></h2><ol>"  >> temp.txt
grep -lr -Fiw 'KeyWord3' * | sed -e 's|.*|<li><a href="http://&">&</a></li>|'  >>  temp.txt
echo "</ol>"   >> temp.txt 

I would like to produce the same results as the output generated by the above, but eliminate the clutter. Please advise on how to properly achieve something like the following:

Var myList = "KeyWord1, KeyWord2, KeyWord3"

while myList; Do
    echo "<HR><BR><B><h2>Search for Keyword:  $myList<BR></B></h2><ol>"  >> temp.txt
    grep -lr -Fiw '$myList' * | sed -e 's|.*|<li><a href="http://&">&</a></li>|'  >>  temp.txt
    echo "</ol>"   >> temp.txt 
; done
  • I am using sh on OS X – David Apr 21 at 18:27
0

Your guess was pretty close.  You can do

for w in 'KeyWord1' 'KeyWord2' 'KeyWord3'
do
    echo "<HR><BR><B><h2>Search for Keyword:  $w<BR></B></h2><ol>"  >> temp.txt
    grep -lr -Fiw "$w" * | sed -e 's|.*|<li><a href="http://&">&</a></li>|'  >>  temp.txt
    echo "</ol>"   >> temp.txt
done

As a matter of style, I would change that to

for w in 'KeyWord1' 'KeyWord2' 'KeyWord3'
do
        echo "<HR><BR><B><h2>Search for $w:  KeyWord1<BR></B></h2><ol>"
        grep -lr -Fiw "$w" * | sed -e 's|.*|<li><a href="http://&">&</a></li>|'
        echo "</ol>"
done  > temp.txt

Your version will append this new content to a pre-existing temp.txt file.  If that’s what you really want to do, change the > back to a >>.  After that, it is (infinitesimally) more efficient to open temp.txt for writing once for the entire loop rather than three times for each keyword.

P.S. It just occurred to me that you have probably written <html><head>…</head><body> to the output file before you do this, so of course you do need this to append to that pre-existing content.

  • And David, please fix your HTML: "<hr/><br/><h2>Search for keyword: $w</h2><br/><ol>" (lowercase tags, removed broken <B> inside <h2/>, moved <br/> after </h2>) – Freddy Apr 21 at 20:00
  • @G-Man Thank you – David Apr 21 at 20:12
0

Hi David — I'd add a comment to G-Man's answer but I don't have the points.

Just wanted to add that with a recursive grep, you probably want to specify a directory or directories to search, not a wildcard, so something more like grep -lr -Fiw "$w" . … instead.

  • Thanks for the comment, but wildcard is ideal as I'm scanning a large volume of directories. – David Apr 21 at 20:02
  • Larry’s point is that grep -r … . is pretty much equivalent to grep -r … *. – G-Man Apr 21 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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