简体   繁体   English

搜索file1中包含的字符串中未找到的字符串

[英]Searching for strings contained in file1 that are NOT FOUND in file2

I'm trying to run a search command to lookup strings from file1 and find them in file2. 我正在尝试运行搜索命令来从file1查找字符串并在file2中找到它们。 I then want to print ONLY the strings from file1 that are NOT FOUND in file2. 然后我想只打印file1中未找到的文件中的字符串。

File1 would be something like: File1将是这样的:

 read
 write
 access

File2 would be: File2将是:

0xFF88 T write
0xFF87 t xyzwrite
0xFF86 T read
0xFF85 T xyzread
0xFF84 T xyzaccess

So the desired result would be: 所以期望的结果是:

 access

*** Note, I did add a blank to all of the strings in File1 in order to not include every occurrence of the string which is part of another string. ***注意,我确实在File1中的所有字符串中添加了一个空格,以便不包括作为另一个字符串一部分的字符串的每次出现。

I've tried: 我试过了:

grep -vf file1 file2 

and get results from file2 that are all but the write and read lines, addresses included. 并从file2获得除写入和读取行之外的所有结果,包括地址。

I've tried: 我试过了:

grep -vf file2 file1 

and get all of file1 because a whole line of file2 never appears in file1. 并获取file1的全部内容,因为整行file2永远不会出现在file1中。

I've tried: 我试过了:

diff file1 file2 | grep \^|<

and get all of file1 proceeded with < on each line. 并使每行继续使用<进行所有file1。

I was told that if I could remove the first 8 characters of each line in file2 then the diff/grep commands would work. 有人告诉我,如果我可以删除file2中每行的前8个字符,那么diff / grep命令就可以了。

I've also tried findstr (Windows) with various options and again, I can't get it to work. 我也尝试过使用各种选项的findstr(Windows),再次,我无法让它工作。

Also, please note that each file has many more lines than I've shown. 此外,请注意,每个文件都有比我显示的更多的行。

Any ideas? 有任何想法吗?

You probably have to do a loop: 你可能要做一个循环:

for i in $(cat File1); do
  grep -q "\<$i\$" File2 || echo $i
done

This will print out: 这将打印出来:

access

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM