简体   繁体   English

使用带有 grep 的查找不隔离文件

[英]Using find with grep not isolating files

I have a sequence of files that look like this:我有一系列看起来像这样的文件:

image-seq 2020-04-13-05-50-00.jpg 
image-seq 2020-04-13-06-00-00.jpg
...
image-seq 2020-04-13-16-50-00.jpg
image-seq 2020-04-13-17-00-00.jpg

I'm trying to separate files with times from 17:00 until 05:50 the following day (so only the first and last filenames in the example should be returned).我正在尝试将时间从 17:00 到次日 05:50 的文件分开(因此仅应返回示例中的第一个和最后一个文件名)。 I thought I could use find with grep to isolate the files and then mv them, but files which shouldn't appear show up in the resulting output.我想我可以使用带有 grep 的 find 来隔离文件,然后对它们进行mv ,但是不应该出现的文件会出现在生成的 output 中。

I'm using macOS grep, so I'm not sure if that's the issue.我正在使用 macOS grep,所以我不确定这是否是问题所在。

The command I tried was:我试过的命令是:

find . -type f -name \*.jpg -exec grep -E '\-1[7-9]|2[0-3]|0[0-5]-\d{2}-\d{2}\.jpg' {} \;

I'm not sure what I've done incorrectly, but if you have any tips that'd be great.我不确定我做错了什么,但是如果您有任何提示,那就太好了。

On macOS find you may use this command to get your matching files using -E and -iregex options:在 macOS 上,您可以使用-Efind选项使用此命令获取匹配的-iregex

find -E . -iregex '.*(-1[7-9]|2[0-3]|0[0-5])(-[0-9]{2}){2}\.jpg'

./image-seq 2020-04-13-05-50-00.jpg
./image-seq 2020-04-13-17-00-00.jpg

Instead of searching inside the files using -exec grep search inside the list of filenames printed by find using a pipe find | grep而不是使用-exec grep文件中搜索,而是使用 pipe find | grep find打印的文件名列表中搜索find | grep . find | grep

Then, you also have to adapt the regex.然后,您还必须调整正则表达式。 With -a|b|c- you search for either -a , b , or c- .使用-a|b|c-您可以搜索-abc- You probably meant -(a|b|c)- which searches for either -a- , -b- , or -c- .您可能的意思是-(a|b|c)-搜索-a--b--c-

find . -type f -name \*.jpg | grep -E -e'-(1[7-9]|2[0-3]|0[0-5])-..-..\.jpg'

However, writing regexes for fitlering number ranges is tiring and error prone.但是,为过滤数字范围编写正则表达式既累人又容易出错。 As you probably know, above regex doesn't check the minutes.您可能知道,上面的正则表达式不检查分钟。 You might want to switch to awk instead:您可能想改用awk

find . -type f -name \*.jpg | awk -F- '$5>=17 || $5<5 || $5==5 && $6<=50'

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

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