[英]How to search a string in multiple files and return file name with line number/text in an Excel or csv in Powershell
[英]How to search multiple files in folder for a string with a minimum number of characters?
在 Powershell 中的 Windows 上,如何在文件夾中的多個文件中搜索最少 16 個字符的行? 此外,有沒有辦法指定字符串只能是數字?
我一直在查看Select-String
,但沒有看到最小字符的選項。
謝謝! 亞歷克斯
以當前目錄 ( .
) 和匹配所有.txt
文件的模式為例:
到 output 匹配文件中至少有 16 個字符的所有行:
Get-Content .\*.txt | Where-Object Length -ge 16
為了額外確保所有這些行僅由十進制數字[1]組成:
Get-Content .\*.txt | Where-Object { $_.Length -ge 16 -and $_ -match '^\d+$' }
筆記:
Get-Content
用元數據修飾,特別是:屬性.PSPath
包含該行的原始文件路徑,而.ReadCount
包含原始文件中基於1
的行號。 以下命令通過Format-Table
使用 output 格式來使這些屬性可見:
Get-Content .\*.txt | Where-Object { $_.Length -ge 16 -and $_ -match '^\d+$' } |
Format-Table -Force @{ Name='Line'; Expression={ $_ } }, PSPath, ReadCount
-Force
需要覆蓋[string]
實例的默認 output 格式(僅打印字符串本身)
哈希表( @{... }
)用於定義輸出行本身的計算屬性,即字符串的內容; 請注意,您可以使用相同的技術重命名屬性,例如將PSPath
重命名為FilePath
,將ReadCount
重命名為LineNumber
。
樣本 output:
Line PSPath ReadCount
---- ------ ---------
1111111111111111 C:\Users\jdoe\foo.txt 10
22222222222222222 C:\Users\jdoe\bar.txt 12
[1] 嚴格來說, \d
匹配所有 Unicode 個被認為是數字的字符,其中包括來自不同書寫系統的數字; 您可以使用[0-9]
來限制對 ASCII 范圍數字的匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.