当前位置:首页> 正文

关于命令行:在Windows上是否可以替换cat

关于命令行:在Windows上是否可以替换cat

Is there replacement for cat on Windows

我需要在Windows上使用*.bat脚本连接两个二进制文件。

我该如何实现?


Windows type命令的工作方式与UNIX cat相似。

范例1:

1
type file1 file2 > file3

等价于:

1
cat file1 file2 > file3

范例2:

1
type  *.vcf > all_in_one.vcf

此命令会将所有vcard合并为一个。


您可以这样使用copy /b

1
copy /b file1+file2 destfile


如果您可以控制正在工作的计算机,则强烈建议安装GnuWin32。只需"全部下载",然后让wget程序检索所有软件包。然后,您将可以访问cat,grep,find,gzip,tar,更少以及数百种其他内容。

GnuWin32是我在新的Windows机器上安装的第一批东西。


无耻的PowerShell插件(因为我认为学习过程很痛苦,因此随时学习可以有所帮助)

1
Get-Content file1,file2

请注意,type是Get-Content的别名,因此,如果您更喜欢它,可以编写:

1
type file1,file2

只需对多个源文件和一个目标文件使用dos copy命令。

1
copy file1+file2 appendedfile

您可能需要/ B选项用于二进制文件


在Windows 10的Redstone 1版本中,Windows为NTOS内核添加了一个真正的Linux子系统。我认为最初是为了支持Android应用程序,也许是Docker类型的方案。微软与Canonical合作,并添加了一个实际的本机bash shell。另外,您可以使用apt软件包管理器来获取许多Ubuntu软件包。例如,您可以像在Linux机器上一样执行apt-get gcc来安装GCC工具链。

如果我上大学时就存在这样的事情,我想我可以在本地Windows bash shell中完成大多数Unix编程任务。


如果您只想在现有文件的末尾附加文本,则可以使用>>管道。例如:

1
echo new text >>existingFile.txt

所以我一直在寻找一种类似的解决方案来保存EOL字符,结果发现没有办法,所以我尽力而为,发挥自己的效用
这是Windows的本地cat可执行文件-https://mega.nz/#!6AVgwQhL!qJ1sxx-tLtpBkPIUx__iQDGKAIfmb21GHLFerhNoaWk

1
2
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

我称它为Sharp-cat,因为它是C#内置的,可以随时使用防病毒软件进行扫描,并且可以根据要求提供源代码


我尝试重新加入已在Linux服务器中拆分的tar存档。

我发现如果在Windows的cmd.exe中使用type,它将导致文件以错误的顺序连接。(即type有时会先放置XXXX.ad,然后放置XXXX.ac,XXXX.aa等。 ..)

因此,我在GitHub https://github.com/sharkdp/bat上找到了一个名为bat的工具,该工具具有Windows版本,并且代码突出显示效果更好,重要的是,在Windows上可以很好地重新加入tar存档!


如果您必须使用批处理脚本并安装了python,则可以在批处理和python中找到一个polygot答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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())

如果另存为join.bat,用法将是:

1
join.bat file_one.bin file_two.bin out_file.bin

也非常感谢这个答案的启发。


展开全文阅读

相关内容