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

15,774

Solution 1

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.

Solution 2

Instead of calling grep multiple times (once per name), use the -f option to find all matches in one call:

grep -f names.txt CallLog.tsv > FilteredCallLogs.tsv

If you need the lines grouped by name, you can sort the result on the proper field:

# E.g., if the names are in column 2
grep -f names.txt CallLog.tsv | sort -k2,2 > FilteredCallLogs.tsv
Share:
15,774
Fr0ntSight
Author by

Fr0ntSight

Updated on June 24, 2022

Comments

  • Fr0ntSight
    Fr0ntSight almost 2 years

    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.

  • Fr0ntSight
    Fr0ntSight almost 11 years
    Can you explain to me the last line?
  • Fr0ntSight
    Fr0ntSight almost 11 years
    I ran the first one and for some reason all of the names were out of order. I will try out the second one with the sorting. Thank you.
  • Fr0ntSight
    Fr0ntSight almost 11 years
    It seems like it works! I have never seen the input at the end like that: <names.txt Very cool. Thanks.
  • DWils
    DWils almost 7 years
    For anyone interested, I turned this into a gist. gist.github.com/uabassguy/f65c6310b68bfefcb626d4d917767f92
  • Ansgar Wiechers
    Ansgar Wiechers almost 7 years
    I have seen way too many gists disappear without a trace to consider that service anywhere near useful.