Answered 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 ""}



  • 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.

  • 2012년 3월 2일 금요일 오전 7:29
     
     답변됨 코드 있음

    Hi

    Or try that way.

    Get-Content p:\test.TXT | Where {($_).trim() -ne ""} | % {$_.trim()}

  • 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 " "

  • 2012년 3월 2일 금요일 오후 4:42
     
     
    awesome, awesome, awesome, many thanks
  • 2012년 3월 2일 금요일 오후 4:45
     
     
    No problem, ask for one solution get four. Today is your lucky day ;)