使用 Linux cmd 将列表名称与一堆文件进行匹配

发布于 2024-11-27 15:20:26 字数 349 浏览 2 评论 0原文

我在匹配文件名时遇到了一个小问题。

我有一个文本文件,其中包含大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水溶 2024-12-04 15:20:26

如果您搜索文件的内容:

#!/bin/sh  
for i in `cat files`
do
    grep -R $i folder --color
done

并且如果您搜索文件的文件名:

#!/bin/sh 
for i in `cat files`
do
find . -name $i*
done

If you search in the content of the files :

#!/bin/sh  
for i in `cat files`
do
    grep -R $i folder --color
done

and if you search in the filename of the files :

#!/bin/sh 
for i in `cat files`
do
find . -name $i*
done
压抑⊿情绪 2024-12-04 15:20:26
for file in $(< list); 
do 
   [ ! -f ${file}xxx ] && echo "x: " ${file}xxx
done

list 是文件,包含文件名“abc acc ...”的列表。
< 是重定向 - 因此我们从文件“list”中读取,与 $(cat list) 相同。如果文件未命名为“list”,只需替换名称即可。
file 在该语句中声明,并迭代地绑定到“list”中的所有这些条目。稍后它被用作${file}。
<代码>[! -f ${file}xxx ] 是一个测试,-f(ile) 是否存在,对于 abc 来说它搜索文件 abcxxx。
但 !否定搜索,因此如果不存在这样的文件,则调用 echo ... 。 “x:”只是一个调试遗留物。

我们可以改进这部分:

for file in $(< list); 
do 
   [ -f ${file}xxx ] || echo ${file}xxx
done

我们可以写“x OR y”,而不是“NOT x AND y”——含义是相同的,只是更短:文件确实存在或回显其名称。

||是短路或。

for file in $(< list); 
do 
   [ ! -f ${file}xxx ] && echo "x: " ${file}xxx
done

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:

for file in $(< list); 
do 
   [ -f ${file}xxx ] || echo ${file}xxx
done

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.

欲拥i 2024-12-04 15:20:26

如果您可以安排文本文件将每个名称放在单独的行上,那么以下命令将满足您的需要:

ls myfolder | grep -f mytextfile

在文本文件中将每个名称放在单独的行上的一种方法是在 中编辑副本vi 并发出命令:(

:%s/ /^V^M/g
:wq

^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:

ls myfolder | grep -f mytextfile

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:

:%s/ /^V^M/g
:wq

(^V^M means type "control-v" then "control-m")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文