[英]grep for two patterns independently (in different lines)
我有一些具有以下结构的目录:
DAY1/ # Files under this directory should have DAY1 in the name.
|-- Date
| |-- dir1 # Something wrong here, there are files with DAY2 and files with DAY1.
| |-- dir2
| |-- dir3
| |-- dir4
DAY2/ # Files under this directory should all have DAY2 in the name.
|-- Date
| |-- dir1
| |-- dir2 # Something wrong here, there are files with DAY2, and files with DAY1.
| |-- dir3
| |-- dir4
在每个dir
中有数十万个名称包含DAY
的文件,例如0.0000.DAY1.01927492
。 名称上带有DAY1
文件应仅出现在父目录DAY1
。
复制文件时出错了,所以我现在在一些dir
目录中有DAY1
和DAY2
混合文件。
我写了一个脚本来查找包含混合文件的文件夹,因此我可以更仔细地查看它们。 我的脚本如下:
for directory in */; do
if ls $directory | grep -q DAY2 ; then
if ls $directory | grep -q DAY1; then
echo "mixed files in $directory";
fi ;
fi;
done
这里的问题是我要经历两次所有文件,考虑到我只需要查看一次文件就没有意义了。
什么是更有效的方式实现我想要的?
如果我理解正确,那么你需要递归地找到DAY1
目录下的文件,它们的名字中有DAY2
,类似于DAY2
目录的文件名称中有DAY1
。
如果是这样,对于DAY1
目录:
find DAY1/ -type f -name '*DAY2*'
这将获得DAY1
目录下名称中包含DAY2
的文件。 同样适用于DAY2
目录:
find DAY2/ -type f -name '*DAY1*'
两者都是递归操作。
仅获取目录名称:
find DAY1/ -type f -name '*DAY2*' -exec dirname {} +
请注意, $PWD
将显示为.
。
要获得唯一性,请将输出传递给sort -u
:
find DAY1/ -type f -name '*DAY2*' -exec dirname {} + | sort -u
鉴于通过它们一次并经历两次之间的差异只是两个因素之间的差异,改为只通过它们一次的方法可能实际上不是一个胜利,因为新方法可能很容易花费两倍每个文件长。
所以你肯定想要试验; 它不一定是你可以自信地推理的东西。
但是,我会说,除了两次浏览文件之外, ls
版本还会对文件进行排序 ,这可能具有超过线性的成本(除非它正在进行某种桶式排序)。 消除,通过编写ls --sort=none
,而不是仅仅ls
,实际上会提高你的算法复杂度,而且几乎肯定将得到明显改善。
但是FWIW,这是一个只能通过文件一次的版本,你可以尝试:
for directory in */; do
find "$directory" -maxdepth 1 \( -name '*DAY1*' -or -name '*DAY2*' \) -print0 \
| { saw_day1=
saw_day2=
while IFS= read -d '' subdirectory ; do
if [[ "$subdirectory" == *DAY1* ]] ; then
saw_day1=1
fi
if [[ "$subdirectory" == *DAY2* ]] ; then
saw_day2=1
fi
if [[ "$saw_day1" ]] && [[ "$saw_day2" ]] ; then
echo "mixed files in $directory"
break
fi
done
}
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.