Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's something that will do what you want, but the only thing is that it will <strong>not</strong> print out the matched word, instead just prints out the line matched, the file name, and the line number. However, if you use <code>--color=auto</code> on grep, it will highlight the matched words using whatever you have set in <code>${GREP_COLOR}</code>, the default is red.</p> <pre><code>cat files.txt | xargs grep -nf words.txt --color=auto </code></pre> <p>This command will dump all contents of <code>files.txt</code>, line by line, and it will pipe the file names to grep, which will search the file for every word that matches in <code>words.txt</code>. Similar to <code>files.txt</code>, <code>words.txt</code> should be all the search terms you want delimited by new-lines.</p> <p>If your grep was built with the perl regular expression engine, then, you can use Perl regular expressions if you pass the <code>-P</code> option to grep like so:</p> <pre><code>grep -Pnf words.txt --color=auto </code></pre> <p>Hope this helps.</p> <p>Update: At first, I wasn't really sure what @Zeophlite was asking but after he posted his example, I see what he wanted. Here's a python implementation of what he wants to do:</p> <pre><code>from contextlib import nested def search_file(line_num, filename): with nested(open(filename), open('words.txt')) as managers: open_filename, word_file = managers for line in open_filename: for wordfile_line_number, word in enumerate(word_file, 1): if word.strip() in line: print "%s =&gt; %s" % (line_num, wordfile_line_number) with open('files.txt') as filenames_file: for filenames_line_number, fname in enumerate(filenames_file, 1): search_file(filenames_line_number, fname.strip()) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload