首页 > 解决方案 > Powerhsell/Unix Bash - 查找一组文件的缺失数字

问题描述

我目前正在尝试批量检测一些丢失的文件。

此批处理生成具有以下约定的文件:

我想创建一个脚本来找出缺少哪个迭代。我在powershell中找到了这段代码

function missingNumbers {
Get-ChildItem -path ./* -include *.txt 
| Where{$_.Name -match '\d+'} 
| ForEach{$Matches[0]} 
| sort {[int]$_} |% {$i = 1}
{while ($i -lt $_){$i;$i++};$i++}}

但是,当我使用此脚本时

 File_1_01.txt
 File_1_02.txt
 File_1_04.txt

它不返回条目 03

调查 删除每小时发生的文件_< 1 >_helps,脚本按预期运行。

如果我连接小时和发生,脚本将显示 101 之前的所有数字。

我也愿意在 Unix 中使用某些东西。

我想到了另一种方法,删除所有文件之间的通用文本,但不知道该怎么做。

标签: powershell

解决方案


您可以匹配最后的 2 位,而不是每次都匹配 1 \d\d。像这样将管道符号放在下一行仅适用于 powershell 7。

Get-ChildItem -path ./* -include *.txt |
  Where Name -match \d\d |
  ForEach{ $Matches[0] } |
  sort {[int]$_} |
  % { $i = 1 } { while ($i -lt $_){ $i;$i++ }; $i++ }


3

你可以像这样把所有的数字放在一起,但是它们必须一个接一个。根据您的需要调整匹配模式或 get-childitem。

Get-ChildItem -path ./* -include *.txt |
  Where Name -match '(\d).(\d\d)' |
  ForEach{ $Matches[1] + $Matches[2] }

101
102
104

推荐阅读