Gir*_*jay 4 powershell cygwin dos2unix
在Windows PowerShell中:
echo "string" > file.txt
Run Code Online (Sandbox Code Playgroud)
在Cygwin:
$ cat file.txt
:::s t r i n g
$ dos2unix file.txt
dos2unix: Skipping binary file file.txt
Run Code Online (Sandbox Code Playgroud)
我想在文件中添加一个简单的"字符串".我该怎么做?即,当我说cat file.txt我只需要"字符串"作为输出.我在Windows PowerShell中回应并且无法更改.
jon*_*n Z 10
尝试echo "string" | out-file -encoding ASCII file.txt获取一个简单的ASCII编码的txt文件.
产生的文件比较:
echo "string" | out-file -encoding ASCII file.txt
Run Code Online (Sandbox Code Playgroud)
将生成一个包含以下内容的文件:
73 74 72 69 6E 67 0D 0A (string..)
Run Code Online (Sandbox Code Playgroud)
然而
echo "string" > file.txt
Run Code Online (Sandbox Code Playgroud)
将生成一个包含以下内容的文件:
FF FE 73 00 74 00 72 00 69 00 6E 00 67 00 0D 00 0A 00 (ÿþs.t.r.i.n.g.....)
Run Code Online (Sandbox Code Playgroud)
(字节顺序标记FF FE表示文件是UTF-16(LE).UTF-16(LE)的签名= 2字节:0xFF 0xFE后跟2字节对.xx 00 xx 00 xx 00表示正常0-127 ASCII字符
这两个命令是等效的,因为它们都默认使用 UTF-16 编码:
echo "string" > file.txt
echo "string" | out-file file.txt
Run Code Online (Sandbox Code Playgroud)
您可以向后一种形式添加显式编码参数(如 jon Z 所示)以生成纯 ASCII:
echo "string" | out-file -encoding ASCII file.txt
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用set-content,它默认使用 ASCII 编码:
echo "string" | set-content file.txt
Run Code Online (Sandbox Code Playgroud)
想要在一行内将 unicode 文件转换为 ASCII?
只需使用这个:
get-content your_unicode_file | set-content your_ascii_file
Run Code Online (Sandbox Code Playgroud)
可以缩写为:
gc your_unicode_file | sc your_ascii_file
Run Code Online (Sandbox Code Playgroud)
想要获得十六进制转储以便真正了解什么是 unicode、什么是 ASCII?
使用 PowerShell.com 上提供的简洁的Get-HexDump函数。完成后,您可以使用以下命令检查生成的文件:
Get-HexDump file.txt
Run Code Online (Sandbox Code Playgroud)
对于任何重要的事情,您可以指定想要输出的列宽以及要处理的文件字节数,如下所示:
Get-HexDump file.txt -width 15 -bytes 150
Run Code Online (Sandbox Code Playgroud)