Using grep to compare two files and make a filter on key words
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using grep to compare two files and make a filter on key words
Hello experts,
I have a problem with two files.
File 1 : one word by line; it is the name of Oracle's tables.
File 2 : many words by line; it is the SELECT * from Oracle's views.
I need to find, in the file 1, all the words that are not in the file 2.
I tried with grep but I failed...
Can you tell me how to do?
Here an extract of File 1 : there are many space characters at the end of the line, we have to remove them in the search.
Here an extract I simplified and modified of File 2.
PHP Code:
SELECT * from WRH$_ACTIVE_SESSION_HISTORY SELECT * from WRH$_ASM_BAD_DISK SELECT col12, col34 from WRH$_ASM_DISKGROUP, WRH$_ASM_BAD_DISK etc etc SELECT * from WRH$_ASM_DISKGROUP_STAT ORDER BY 1
Sorry, I don't see where I can insert a whole file (is it possible?) I don't want to copy ALL the content of my files, it is too big...
And sorry if I made mistakes for my first post here ...
You're probably going to need an awk or perl script with an associative array. Read the shorter word list into the array and then run the second, larger file through it.
You could write a small Python script/program that takes two files as arguments and spits out a difference file and a file with everything that matches. I feel like that would be the best short-term solution, assuming you need to do this for the mentioned big file(s).
Thank you Turbocapitalist for Awk, I've forgotten that I have a book about it...
Tomorrow I will read it, I think it will be very useful to solve my problem.
Having the pattern in an unpredicable place on each line and possibly multiple times means you'll probably have to work out something with the match() function or similar.
while read -r line; do
grep -wFq "$line" queries.txt || echo "$line"
done < tables.txt
tables.txt is your file 1 - the list of tables (needles) to search for, with $line being each one.
queries.txt is your file 2 - the queries file (haystack) to search within.
The first and last lines are for looping through the file (for variations see BashFAQ/001).
Grep flags:
-w matches whole words (addresses suffixed names)
-F is for Fixed strings - i.e. disables regex matching
-q is for quiet - i.e. don't output when matches found (we want the opposite)
The || is so that when grep doesn't match, the line searched for is then output.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.