[英]Find missing files by their number?
I have a big list of ordered files with names like this 我有大量的有序文件,其名称如下
file_1.txt
file_2.txt
file_3.txt
file_6.txt
file_7.txt
file_8.txt
file_10.txt
In this case it is easy to see that files: file_4.txt
, file_5.txt
and file_9.txt
are missing, but if i have a big list how can i find the missing files? 在这种情况下,很容易看到文件:
file_4.txt
, file_5.txt
和file_9.txt
丢失了,但是如果我的清单很大,我如何找到丢失的文件? i am just learning bash so i just know some simple examples. 我只是在学习bash,所以我只知道一些简单的例子。 like this
像这样
for i in $(seq 1 1000) ;
do
if [i not in *.txt]; then
echo $i;
done
But this doesnt even work unless i erase the if [i not in *.txt];then
line so it just writes all the numbers between 1 and 1000. I hope you can help me. 但是,除非我先删除
if [i not in *.txt];then
删除该行,否则它只写1到1000之间的所有数字,否则甚至不起作用。希望您能对我有所帮助。 Thanks in advance. 提前致谢。
If they are in a file then this should work 如果它们在文件中,那么应该可以
awk 'match($0,/([0-9]+)/,a){a[1]>max&&max=a[1];b[a[1]]++}
END{for(i=1;i<max;i++)if(!b[i])print "file_"i".txt"}' file
file_4.txt
file_5.txt
file_9.txt
One way to do this is by 一种方法是
## TODO: You need to change the following path:
THELIST=/path/to/input-file
for i in $(seq 1 10);
do
FOUND=`grep "file_$i.txt" "$THELIST"` #look for file $i in $THELIST
#Note: double quotes were placed around $THELIST
# in case there is whitespace in the filename
[[ "$FOUND" == "" ]] && echo $i #if what you found is empty, then output $i
done
[[ ... ]]
here: What is the difference between single and double square brackets in Bash? [[ ... ]]
信息: Bash中的单方括号和双方括号有什么区别? square-brackets The suggestion from @user4453924 really helped me out. @ user4453924的建议确实帮助了我。 It does not have to be in a file, just pipe output from ls into his awk command, and you should be fine:
它不必在文件中,只需将ls的输出通过管道传输到他的awk命令中,就可以了:
ls *.txt | awk 'match($0,/([0-9]+)/,a){a[1]>max&&max=a[1];b[a[1]]++}
END{for(i=1;i<max;i++)if(!b[i])print "file_"i".txt"}'
Outputs: 输出:
file_4.txt
file_5.txt
file_9.txt
Alternatively, if you prefer to do it in a two step fashion, it would be quite simple to pipe the output from ls into a file, and then use his command directly on the file, as it is: 另外,如果您希望以两步方式进行操作,则将ls的输出通过管道传输到文件中,然后直接在文件上使用他的命令非常简单,因为它是:
ls *.txt > filelist.txt
awk 'match($0,/([0-9]+)/,a){a[1]>max&&max=a[1];b[a[1]]++}
END{for(i=1;i<max;i++)if(!b[i])print "file_"i".txt"}' filelist.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.