file in Windows PowerShell adds a non-printable character to a file In Windows PowerShell: echo "string" > file.txt In Cygwin:...">

Echo "string"> file in Windows PowerShell adds a non-printable character to a file

In Windows PowerShell:

echo "string" > file.txt 

In Cygwin:

 $ cat file.txt :::string $ dos2unix file.txt dos2unix: Skipping binary file file.txt 

I want a simple "line" in a file. How can I do it? Ie when I say cat file.txt I only need a "string" as the output. I echo from Windows PowerShell and cannot be modified.

+4
source share
3 answers

Try echo "string" | out-file -encoding ASCII file.txt echo "string" | out-file -encoding ASCII file.txt get a plain text file in ASCII format.

Comparison of received files:

 echo "string" | out-file -encoding ASCII file.txt 

will create a file with the following contents:

 73 74 72 69 6E 67 0D 0A (string..) 

but

 echo "string" > file.txt 

will create a file with the following contents:

 FF FE 73 00 74 00 72 00 69 00 6E 00 67 00 0D 00 0A 00 (ΓΏΓΎs.tring....) 

(The byte sign of FF FE indicates that the file is UTF-16 (LE). Signature for UTF-16 (LE) = 2 bytes: 0xFF 0xFE and then 2 byte pairs. Xx 00 xx 00 xx 00 for normal 0 -127 ASCII characters

+8
source

These two commands are equivalent in that they both use the default UTF-16 encoding:

 echo "string" > file.txt echo "string" | out-file file.txt 

You can add an explicit encoding parameter to the last form (as pointed out by jon Z) to create a simple ASCII:

 echo "string" | out-file -encoding ASCII file.txt 

Alternatively, you can use set-content , which uses ASCII encoding by default:

 echo "string" | set-content file.txt 

Corollary 1:

Want to convert a Unicode to ASCII file in one line?

Just use this:

 get-content your_unicode_file | set-content your_ascii_file 

which can be shortened to:

 gc your_unicode_file | sc your_ascii_file 

Corollary 2:

Want to get a hex dump so you can really see what unicode is and what ASCII is?

Use the clean and simple Get-HexDump function available on PowerShell.com. With this, you can check your generated files only with:

 Get-HexDump file.txt 

For something non-trivial, you can specify how many columns you need and how many bytes of the file to process with something like this:

 Get-HexDump file.txt -width 15 -bytes 150 
+4
source

PowerShell creates Unicode UTF-16 files with bytes (BOM).

Dos2unix 6.0 and higher can read UTF-16 files and convert them to UTF-8 (Cygwin encoding by default) and delete the specification. Versions prior to 6.0 will see UTF-16 files as binary and skip them, as in your example.

0
source

Source: https://habr.com/ru/post/1381635/


All Articles