Read lines from a file then search a different file using those values

bash, cat, csv, grep

Solution

Try something like this:

#!/bin/bash

while read name; do
  grep "$name" CallLog.tsv
done <names.txt >FilteredCallLogs.tsv

`<names.txt` feeds `names.txt` into the loop where `while read name` reads it line-by-line into the loop variable `$name`. `>FilteredCallLogs.tsv` redirects the output from the loop into the file `FilteredCallLogs.tsv`.

Problem

I have a file with a list of names in it (`names.txt`) and I have a file with thousands of lines of tab seperated values (`CallLog.tsv`). I need to `grep` each name in `names.txt` using the `CallLog.tsv` file and then save that as a new file. Right now I am doing the names individually: ``` grep "John" CallLog.tsv > JohnCallLogs ``` Then I am taking all the names and `cat`'ing them to another file: ``` cat "John" "Mike" "Dave > FilteredCallLogs ``` I want to write a script to make this more efficient. I appreciate any help.

Original source