Windows 上是否有 cat 的替代品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/60244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there replacement for cat on Windows
提问by Artem Tikhomirov
I need to join two binary files with a *.bat
script on Windows.
我需要*.bat
在 Windows 上用脚本连接两个二进制文件。
How can I achieve that?
我怎样才能做到这一点?
回答by Nathan Jones
Windows type
command works similarly to UNIX cat
.
Windowstype
命令的工作方式与 UNIX 类似cat
。
Example 1:
示例 1:
type file1 file2 > file3
is equivalent of:
相当于:
cat file1 file2 > file3
Example 2:
示例 2:
type *.vcf > all_in_one.vcf
This command will merge all the vcards into one.
此命令会将所有 vcard 合并为一个。
回答by Greg Hewgill
You can use copy /b
like this:
你可以这样使用copy /b
:
copy /b file1+file2 destfile
回答by David Citron
If you have control over the machine where you're doing your work, I highly recommend installing GnuWin32. Just "Download All" and let the wget program retrieve all the packages. You will then have access to cat, grep, find, gzip, tar, less, and hundreds of others.
如果您可以控制工作所在的机器,我强烈建议您安装GnuWin32。只需“下载全部”并让 wget 程序检索所有包。然后,您将可以访问 cat、grep、find、gzip、tar、less 和数百个其他文件。
GnuWin32 is one of the first things I install on a new Windows box.
GnuWin32 是我在新的 Windows 机器上安装的第一批东西之一。
回答by Jay Bazuzi
Shameless PowerShell plug (because I think the learning curve is a pain, so teaching something at any opportunity can help)
无耻的 PowerShell 插件(因为我认为学习曲线很痛苦,所以在任何机会教一些东西都会有所帮助)
Get-Content file1,file2
Note that type
is an alias for Get-Content, so if you like it better, you can write:
注意这type
是 Get-Content 的别名,所以如果你更喜欢它,你可以写:
type file1,file2
回答by simon
Just use the dos copy command with multiple source files and one destination file.
只需对多个源文件和一个目标文件使用 dos copy 命令。
copy file1+file2 appendedfile
You might need the /B option for binary files
您可能需要二进制文件的 /B 选项
回答by sam msft
In Windows 10's Redstone 1 release, the Windows added a real Linux subsystem for the NTOS kernel. I think originally it was intended to support Android apps, and maybe docker type scenarios. Microsoft partnered with Canonical and added an actual native bash shell. Also, you can use the apt package manager to get many Ubuntu packages. For example, you can do apt-get gcc to install the GCC tool chain as you would on a Linux box.
在 Windows 10 的 Redstone 1 版本中,Windows 为 NTOS 内核添加了一个真正的 Linux 子系统。我认为最初它是为了支持 Android 应用程序,可能还有 docker 类型的场景。微软与 Canonical 合作并添加了一个真正的原生 bash shell。此外,您可以使用 apt 包管理器来获取许多 Ubuntu 包。例如,您可以像在 Linux 机器上一样执行 apt-get gcc 来安装 GCC 工具链。
If such a thing existed while I was in university, I think I could have done most of my Unix programming assignments in the native Windows bash shell.
如果我在大学时有这样的东西存在,我想我可以在本地 Windows bash shell 中完成我的大部分 Unix 编程任务。
回答by Jahmic
If you simply want to append text to the end of existing file, you can use the >> pipe. ex:
如果您只想将文本附加到现有文件的末尾,您可以使用 >> 管道。前任:
echo new text >>existingFile.txt
回答by Noelkd
If you have to use a batch script and have python installed here is a polygotanswer in batch and python:
如果您必须使用批处理脚本并在此处安装了 python,则批处理和 python 中的polygot答案是:
1>2# : ^
'''
@echo off
python "%~nx0" " %~nx1" "%~nx2" "%~nx3"
exit /b
rem ^
'''
import sys
import os
sys.argv = [argv.strip() for argv in sys.argv]
if len(sys.argv) != 4:
sys.exit(1)
_, file_one, file_two, out_file = sys.argv
for file_name in [file_one, file_two]:
if not os.path.isfile(file_name):
print "Can't find: {0}".format(file_name)
sys.exit(1)
if os.path.isfile(out_file):
print "Output file exists and will be overwritten"
with open(out_file, "wb") as out:
with open(file_one, "rb") as f1:
out.write(f1.read())
with open(file_two, "rb") as f2:
out.write(f2.read())
If saved as join.bat usage would be:
如果保存为 join.bat 用法将是:
join.bat file_one.bin file_two.bin out_file.bin
Thanks too this answerfor the inspiration.
也感谢这个答案的灵感。
回答by Aaron Xu
I try to rejoin tar archive which has been splitted in a Linux server.
我尝试重新加入已在 Linux 服务器中拆分的 tar 存档。
And I found if I use type
in Windows's cmd.exe
, it will causes the file being joined in wrong order.(i.e. type
sometimes will puts XXXX.ad at first and then XXXX.ac , XXXX.aa etc ...)
我发现如果我type
在 Windows 中使用cmd.exe
,它会导致文件以错误的顺序加入。(即type
有时会先放 XXXX.ad ,然后放 XXXX.ac , XXXX.aa 等...)
So, I found a tool named bat
in GitHub https://github.com/sharkdp/batwhich has a Windows build, and has better code highlight and the important thing is, it works fine on Windows to rejoin tar archive!
所以,我bat
在 GitHub 中找到了一个名为https://github.com/sharkdp/bat的工具,它具有 Windows 版本,并且具有更好的代码突出显示,重要的是,它在 Windows 上可以正常工作以重新加入 tar 存档!
回答by Ricky Divjakovski
So i was looking for a similar solution with the abillity to preserve EOL chars and found out there was no way, so i do what i do best and made my own utillity This is a native cat executable for windows - https://mega.nz/#!6AVgwQhL!qJ1sxx-tLtpBkPIUx__iQDGKAIfmb21GHLFerhNoaWk
因此,我一直在寻找具有保留 EOL 字符能力的类似解决方案,但发现没有办法,所以我尽我所能并制作了自己的实用程序 这是 Windows 的原生 cat 可执行文件 - https://mega。 nz/#!6AVgwQhL!qJ1sxx-tLtpBkPIUx__iQDGKAIfmb21GHLFerhNoaWk
Usage: cat file1 file2 file3 file4 -o output.txt
-o | Specifies the next arg is the output, we must use this rather than ">>" to preserve the line endings
I call it sharp-cat as its built with C#, feel free to scan with an antivirus and source code will be made available at request
我称它为sharp-cat,因为它是用C#构建的,可以随意使用防病毒软件进行扫描,源代码将根据要求提供