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. Quote:
PHP Code:
Quote:
And sorry if I made mistakes for my first post here ... Have a very nice day. |
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 for yours answers but I am an Oracle DBA, not an expert Linux... I don't know how to use Python or Perl or Awk :-(
|
No time like the present. It's pretty much not possible to do system adminsitration without periodically needing some awk or perl or (maybe) python.
AWK is a full language and would take ages to master but getting an idea of the basics can take but a few minutes: https://www.grymoire.com/Unix/Awk.html AWK in its most elementary form is just a buch of abbreviated if-then statements: Code:
awk ' |
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.
|
Unfortunately the following simple word grep returns the non-matching lines from file2
Code:
grep -vwf file1 file2 |
Doing this with grep isn't difficult:
Code:
while read -r line; do 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. |
boughtonp, you are GREAT!
It works :) Thank you everybody for your help, I was reading a book about Awk but I see we can manage my problem with grep! Have a nice day :-) |
See man diff.
|
All times are GMT -5. The time now is 10:22 AM. |