Convert to ANSI UNIX

This topic contains 5 replies, has 2 voices, and was last updated by  Don Jones 1 year, 2 months ago.

  • Author
  • #46517

    Ricardo Bragança

    I have a txt file and I make some changes and the objective is return a txt file with the same format.
    My problem is that I can not do it to have the same format and I've tried all kinds of encoding, but the result is " ? " instead of " C" or "A".
    How can I solve the problem?

  • #46744

    Don Jones

    What have you tried?

    • #46748

      Ricardo Bragança

      I 'm sorry I was wrong because previously said the result was " ? " instead of " C" or " A" and actually appeared "?" instead of " ç" or "à".
      I've tried using the after and before -path $files -encoding utf8 , unicode , ASCII but the result was always the same.

  • #46751

    Don Jones

    I've never had a problem with -Encoding not producing the desired file, except in cases where the input file wasn't, in fact, what I thought it was, or when the underlying OS file system was doing something goofy to me without me realizing it. I would not expect "-Encoding ASCII" to do what you want, as ASCII isn't a DBCS. I'm assuming you're after ANSI as the character format; the difference between Windows and UNIX would normally be the line endings. But most PowerShell commands don't support ANSI as a named encoding set, so I'm not sure if you're wanting to use Unicode, or UTF8, or what. So it'd be nice to know which named encoding set you're hoping for.

    And, when I asked, "what have you tried," I was hoping to see the actual command(s) you're using. It's a little difficult to help you troubleshoot a problem when I don't know what you're actually doing. Like, I don't even know if you're using Out-File or Set-Content or something else entirely.

    • #46757

      Ricardo Bragança

      I'll try to better explain the goal.
      My input txt file under Notepad ++ is ANSI UNIX type and the goal is the output txt file to have the same ANSI UNIX format.
      And that's what I'm not able to do. The result is a different kind which leads to display " ? " instead of "ç" or "á" e.g.

      in relation to the code I have this:

      $files = Get-ChildItem "C:\Users\ricar\Desktop\NEW\*.txt"
      $folder = "C:\Users\ricar\Desktop\NEW\dados*.txt"
      $count = $null
      switch -Regex -File $files.fullname
          'MAI000' {$count++;Write-Verbose "Group $count found" -Verbose}
          '^FE|^01' { $_ |Add-Content -Path "C:\Users\ricar\Desktop\NEW\dados$($count).txt" -Encoding Ascii }
          'MAIFIM' {continue}
      Get-ChildItem -path $folder -Recurse |
      Where-Object {
            (Select-String -InputObject $_ -Pattern '@' -Quiet) -eq $true
      } |
      ForEach-Object {
          Get-Content $_ | Add-Content C:\Users\ricar\Desktop\NEW\dados.txt -Encoding Ascii
  • #46761

    Don Jones

    So, ASCII isn't ANSI – by specifying ASCII encoding, you're definitely losing.

    But might be helpful. The problem is, regardless what Notepad++ is showing, "ANSI" isn't an encoding. It's a family of encodings, and they're all a little different.

    And the -Content cmdlets only support a limited set of encodings:

    — Unknown
    — String
    — Unicode
    — Byte
    — BigEndianUnicode
    — UTF8
    — UTF7
    — UTF32
    — Ascii
    — Default
    — Oem
    — BigEndianUTF32

    It's possible the exact ANSI variant your file is in *isn't supported* as an output, which means "you can't get there from here." Ascii is definitely not what you want, though. "Default" would be ANSI, using the current system code page, but on Windows that's likely to be different from Unix. PowerShell has some known weak points when it comes to outputting some of the common ANSI variants used on Unix systems, and people often have to build fairly lengthy manual workarounds.

You must be logged in to reply to this topic.