[英]How to use PowerShell to remove space from the end of a line in a text file
[英]Use PowerShell to determine length of a line in a text file
我是PowerShell的新手,但我想使用它,因為在Windows批處理中沒有一種簡單的方法來獲取字符串的長度。
我需要在PowerShell中編寫一段代碼,以遍歷.txt文件中的每一行並確定該行的字符長度。 如果字符長度超過250,則...等。
.... etc部分目前並不重要:)
在Windows批處理中,我會這樣寫:
FOR /F %%A IN ("C:\TestFile.txt") DO (
SET LINE=%%A
If LINE > 250 characters then ( ' This line is made up
....etc
)
我該怎么做?
以下將執行您想要的操作:
$data = Get-Content "C:\TestFile.txt"
foreach($line in $data)
{
if ($line.Length -gt 250) {
Write-Host "A match"
}
}
嘗試這個:
:: Not fully tested:
for /f "delims=" %%s in (C:\TestFile.txt) do (
set "x=%%s" & set /A y+=1
setlocal enabledelayedexpansion
for /f "skip=1 delims=:" %%i in ('"(set x&echo()|findstr /o ".*""') do set/a n=%%i-4
if !n! gtr 250 echo Line !y! Length !n!
endlocal
)
今天正在尋找它,並在這里找到了一個優雅的解決方案: https : //softwarerecs.stackexchange.com/questions/38934/finding-the-longest-line-of-a-document/38936
GC "c:\folder\file.txt" | Measure -Property length -Maximum | Select Maximum
GC "c:\folder\file.txt" | Sort -Property length | Select -last 1
重要提示:信譽來自上述鏈接,位於Pimp Juice IT上,我只是在復制/粘貼:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.