powershell - Powerhsell/Unix Bash - 查找一组文件的缺失数字
问题描述
我目前正在尝试批量检测一些丢失的文件。
此批处理生成具有以下约定的文件:
- File_1_01.txt(第一个小时,第一次迭代)
- 文件_1_02.txt
我想创建一个脚本来找出缺少哪个迭代。我在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 中使用某些东西。
我想到了另一种方法,删除所有文件之间的通用文本,但不知道该怎么做。
解决方案
您可以匹配最后的 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
推荐阅读
- python - Numpy子集和赋值
- windows - 在循环内构建自定义 POSobjects
- google-apps-script - 附加行输出“[Ljava.lang.Object;@536f3e11”而不是范围值
- google-chrome - Netflix 如何阻止用户截取 chrome 浏览器的截图?
- c# - 如何在 Unity 2D (C#) 中将鼠标输入更改为按键输入
- kubernetes - 在 Kubernetes 中运行后台作业
- java - JTable 锁定在 JScrollPane 的顶部
- python - 将列表排序为字典中的值(类似于 pandas.sort_values)
- javascript - 尝试获取资源时出现网络错误类型错误
- java - POST array of entities to a property of _Links ? Spring Data REST