removing blanks from file
-
2012년 3월 2일 금요일 오전 6:58
I have a script that will get-content on a line-by-line text file for a list of servers. I need to remove blank lines, and lines that have blank spaces, as well as blank spaces after server names.
So far I have:
$oldlist = Get-Content $file | Where {$_ -ne ""} | ForEach-Object {$_.trim()} | Out-File ".\newfile.txt"
$newlist = Get-Content ".\newfile.txt" | Where {$_ -ne ""}can this be accomplished in one line, and w/out creating a new text file?
모든 응답
-
2012년 3월 2일 금요일 오전 7:16
Hi, you can. Try this:
$newlist = get-content finish.txt | where {$_} | foreach {$_.trim()} | where {$_ -ne ""}
- 편집됨 Jaap Brasser 2012년 3월 2일 금요일 오전 7:16
- 답변으로 표시됨 Yan Li_Microsoft Contingent Staff, Moderator 2012년 3월 5일 월요일 오전 6:14
-
2012년 3월 2일 금요일 오전 7:29
Another way to achieve the same
Get-Content $File | %{if($_.trim().Length -gt 0) {$_.trim()}}Thanks & Regards
Bhavik Solanki
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.- 답변으로 표시됨 Yan Li_Microsoft Contingent Staff, Moderator 2012년 3월 5일 월요일 오전 6:14
-
2012년 3월 2일 금요일 오전 7:29
Hi
Or try that way.
Get-Content p:\test.TXT | Where {($_).trim() -ne ""} | % {$_.trim()}
- 답변으로 제안됨 Bhavik Solanki 2012년 3월 3일 토요일 오후 8:29
- 답변으로 표시됨 Yan Li_Microsoft Contingent Staff, Moderator 2012년 3월 5일 월요일 오전 6:14
-
2012년 3월 2일 금요일 오전 11:39
Another possibility(Get-Content $file) -match '\S' -replace '^\s*(\S+)'\s*','$1'
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
- 답변으로 표시됨 Yan Li_Microsoft Contingent Staff, Moderator 2012년 3월 5일 월요일 오전 6:14
-
2012년 3월 2일 금요일 오후 4:42awesome, awesome, awesome, many thanks
-
2012년 3월 2일 금요일 오후 4:45No problem, ask for one solution get four. Today is your lucky day ;)

