Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

cygwin - echo "string" > file in Windows PowerShell appends non-printable character to the file

In Windows PowerShell:

echo "string" > file.txt

In Cygwin:

$ cat file.txt
:::s t r i n g

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

I want a simple "string" in the file. How do I do it? I.e., when I say cat file.txt I need only "string" as output. I am echoing from Windows PowerShell and that cannot be changed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try echo "string" | out-file -encoding ASCII file.txt to get a simple ASCII-encoded txt file.

Comparison of the files produced:

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

will produce a file with the following contents:

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

however

echo "string" > file.txt

will produce a file with the following contents:

FF FE 73 00 74 00 72 00 69 00 6E 00 67 00 0D 00 0A 00 (?ts.t.r.i.n.g.....)

(Byte order mark FF FE indicates the file is UTF-16 (LE). The signature for UTF-16 (LE) = 2 bytes: 0xFF 0xFE followed by 2 byte pairs. xx 00 xx 00 xx 00 for normal 0-127 ASCII chars


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...