使用 Linux cmd 将列表名称与一堆文件进行匹配
我在匹配文件名时遇到了一个小问题。
我有一个文本文件,其中包含大约 160 个姓名。我有一个包含 2000 多个文件的文件夹。其中一些包含这160个名字。我正在寻找一个 grep cmd,它可以获取文本文件中的每个名称并尝试将其与文件夹的内容匹配。
我正在尝试用 perl 或直接的 Linux cmd 来完成此操作,但这两种方法对我来说效果都不好,因为我对它们都不熟悉。
例如:文本文件包含
abc acc eee fff
abcXXX、accXXX、eeeXXX 和 fffXXX
我需要对列表进行排序并找出缺少哪一个。
谢谢 戴维
I have a slight problem with matching file names.
I have one text file that contains some 160 names. I have a folder with 2000+ files. and some of them contains these 160 names. I am looking for a grep cmd that can take each name in the text file and try to match it to the contents of the folder.
I am trying to do this in perl, or just straight forward linux cmds, but neither has worked out very well for me because I am not familiar with either of them.
so for example: the text file contains
abc acc eee fff
and the folder will have abcXXX, accXXX, eeeXXX and fffXXX
I need to sort through the list and find out which one were missing.
thx
Davy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您搜索文件的内容:
并且如果您搜索文件的文件名:
If you search in the content of the files :
and if you search in the filename of the files :
list
是文件,包含文件名“abc acc ...”的列表。<
是重定向 - 因此我们从文件“list”中读取,与$(cat list)
相同。如果文件未命名为“list”,只需替换名称即可。file
在该语句中声明,并迭代地绑定到“list”中的所有这些条目。稍后它被用作${file}。<代码>[! -f ${file}xxx ] 是一个测试,-f(ile) 是否存在,对于 abc 来说它搜索文件 abcxxx。
但 !否定搜索,因此如果不存在这样的文件,则调用
echo ...
。 “x:”只是一个调试遗留物。我们可以改进这部分:
我们可以写“x OR y”,而不是“NOT x AND y”——含义是相同的,只是更短:文件确实存在或回显其名称。
||是短路或。
list
is the file, containing the list of filenames "abc acc ...".<
is redirection - so we read from the file 'list', the same as$(cat list)
. If the file isn't named 'list', just replace the name.file
is declared in that statement and iteratively bound to all those entries in 'list'. Later it is used as ${file}.[ ! -f ${file}xxx ]
is a test, whether a -f(ile) exists, for abc it searchs for a file abcxxx.But ! negates the search, so if no such file exists, then
echo ...
is called. "x: " is just a debug relict.We can improve that part:
instead of 'NOT x AND y' we can write 'x OR y' - the meaning is the same, just shorter: the file does exist or echo its name.
|| is short-circuit OR.
如果您可以安排文本文件将每个名称放在单独的行上,那么以下命令将满足您的需要:
在文本文件中将每个名称放在单独的行上的一种方法是在
中编辑副本vi
并发出命令:(^V^M
表示先键入“control-v”,然后键入“control-m”)If you can arrange for the text file to have each name on a separate line then the following command will do what you need:
One way to get each name on a separate line in the text file would be to edit a copy in
vi
and issue the commands:(
^V^M
means type "control-v" then "control-m")