0

Looking for advice on how to search one file for all occurances of a list in another file and output to a results file.

The source file being searched is a list of short names, FQDNS (A records and CNAME's) from a DNS server. The input file is a list of short names which I want to match any occurance. For example if the search list contains:

host123

I'd like it to find all records containing that, so for example:

host123.example.com
host123-idrac.example.com
host123-backup.example.com
test-cname.example.com > host123.example.com

I'm a bit new to this kindof thing so although I maybe able to do this with grep I was hoping to learn a different, maybe more elegant way to do this.

4
  • Welcome to SE ! Please, tell us: (1) what shell you use, issuing $ echo $SHELL at your terminal prompt, (2) an example of pattern list format, (3) whether upper or lower case is significant (I imagine it is not, given your use case).
    – Cbhihe
    May 18 '20 at 20:24
  • @alfapersius Consider adding clarifications to the text of the question, not in comments.
    – Kusalananda
    May 18 '20 at 20:45
  • Exact cross-dupe of stackoverflow.com/a/43042366. Very similar to unix.stackexchange.com/q/397612/72707
    – Cbhihe
    May 18 '20 at 20:48
  • thanks, answers are: 1. bash 2. pattern list is just a text file of short hostnames so example would be: host123 host456 host789 and the file being searched is a csv with shortname and FQDN, for example: host123, host123.example.com host456, host456.example.com 3. case should being ignored as there is a mixture. May 18 '20 at 20:53
0

I think grep its the way to go, for efficiency you can use "grep -f" which is faster that placing a for loop or something like that. You can use awk just for fun or 2 nested for loops

grep -f search_list_file database_file
2
  • OP asked for redirecting output to a result file.
    – Cbhihe
    May 18 '20 at 20:24
  • You may also want to use -F to use the strings in the list file as strings rather than as regular expressions. You also mention something about using awk, but you never say how.
    – Kusalananda
    May 18 '20 at 20:43
0

If your searched patterns are one to a line in a file, say patterns, the searched file is named searched_file, and the result is dumped in result.out:

$ awk 'FNR==NR {a[tolower($0)]=1;next} {for (i in a) if(index(tolower($0),i)) print }' \
  patterns searched_file > result.out

Case insensitive search adapted from an answer by JamesBrown on stackoverflow.com. This was tested on bash.

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.