我有一個C:\FindPos.txt
用這個內容呼叫的檔案:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut.
我有一個偏移量并想找到行和列
- 單詞incididunt以 Offset = 98 結尾。我想找到 Ln = 3 和 Col = 26

目前的實驗:
$path = 'C:\FindPos.txt'
$oneStringcontent = [System.IO.File]::ReadAllText($path)
$fileContent = get-content -Path $path
$StartLine = 3
$StartColumn = 26
$StartOffset = 98
write-host "$($oneStringcontent[$StartOffset 1])"
$fileContent | ForEach-Object {
$currentLine = $_.ReadCount
if ($currentLine -eq $StartLine) {
write-host "Line $StartLine | Column = $StartColumn | Line = $($_) | Char = $($_[$StartColumn 1])"
}
}
如果我將檔案作為一個字串讀取,我可以從陣列中獲取正確的字符,但不知道如何將其轉換為 Ln/Col。
如果我逐行閱讀,我會遇到相反的問題(另外,字符列位置不匹配)
uj5u.com熱心網友回復:
希望以下內容對您有用。
$Path = 'C:\FindPos.txt'
$Content = Get-Content -Path $Path
$Column = $OffSet = 1
$Line = 0
foreach ($Char in $Content.ToCharArray()) {
$CurrentLine = $Content[$Line]
[PSCustomObject] @{
Token = $Char
OffSet = $OffSet
Line = $Line 1
Column = $Column
}
if ($Column -ge $CurrentLine.Length) { $Column = 0; $Line }
$OffSet
$Column
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/527819.html
