Read lines from a file then search a different file using those values
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
Fr0ntSight
Updated on June 24, 2022Comments
-
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 togrep
each name innames.txt
using theCallLog.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 almost 11 yearsCan you explain to me the last line?
-
Fr0ntSight almost 11 yearsI 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 almost 11 yearsIt seems like it works! I have never seen the input at the end like that: <names.txt Very cool. Thanks.
-
DWils almost 7 yearsFor anyone interested, I turned this into a gist. gist.github.com/uabassguy/f65c6310b68bfefcb626d4d917767f92
-
Ansgar Wiechers almost 7 yearsI have seen way too many gists disappear without a trace to consider that service anywhere near useful.