Get-Content pulling in []'s

This topic contains 4 replies, has 3 voices, and was last updated by  Aaron 1 week, 3 days ago.

  • Author
    Posts
  • #81920

    Aaron
    Participant

    Hello,

    I'm doing a get-content on a file and on some blank lines (at least they look blank in the notepad file) are importing in with squares.

    This is a copy/paste of them: here

    Is there a way to do a get-content that will leave these pesky squares out?

  • #81923

    Juan
    Participant

    May be an issue with the encoding. You can try outputting the content to a new file with ASCII

    Get-content testfile.txt | Out-File -FilePath newfile.txt -Encoding ASCII
    
  • #81926

    postanote
    Participant

    There are several way to use Get-Content and manipulate input on read as well as read and write.

    Get-Content -Path D:\Temp -Raw

    (Get-Content -Path D:\Temp) -replace '[]'

    # This will remove empty lines or lines with only whitespace characters (tabs/spaces).
    [IO.File]::ReadAllText("FileWithEmptyLines.txt") -replace '\s+\r\n+', "`r`n" `
    | Out-File "c:\FileWithNoEmptyLines.txt"

    # This removes trailing whitespace and blank lines from file.txt
    PS C:\Users\> (gc file.txt) | Foreach {$_.TrimEnd()} | where {$_ -ne ""} | Set-Content file.txt

    # Or
    (Get-Content c:\FileWithEmptyLines.txt) `
    | ForEach { $_ -Replace "Old content", " New content" } `
    | Set-Content c:\FileWithEmptyLines.txt;

    (get-content 'c:\FileWithEmptyLines.txt') -notmatch '^\s*$' > c:\FileWithNoEmptyLines.txt

  • #81995

    Aaron
    Participant

    After looking at the .txt file it has a symbol in it that is showing up as the square in powershell console.

    As shown here.

    Is there a way to ignore this char? It pastes into the console as nothing

  • #82054

    Aaron
    Participant

    was able to find it by using the below method.

    $lines = get-content c:\filepath\text.txt
    

    The last line was showing up with the [] square symbol, so I did a

    PS C:\Users\user> $lines[-1]
    [] #note, not the actual symbol, it is just a square
    #found it's ascii value by doing
    PS C:\Users\user> [byte][char]$lines[-1]
    12
    

    then just parsed through the text file using

    $lines = get-content c:\filepath\text.txt | ? {$_ -like "bk5013*"}).FullName | ? {$_ -notlike [char]12}
    

    Thanks for the help!

You must be logged in to reply to this topic.