linux - 如何使用dos批处理删除文件中的Ctrl-Z

标签 linux batch-file copy dos cat

我发现每次我用

将所有文件连接到一个文件中
copy *.txt all.txt

,文件末尾有一个 Ctrl-Z 字符。我不想要这个角色。有没有办法编写批处理脚本来删除该字符或避免这种情况发生?

我想做的是

cat *.txt > all.txt 

在 Linux 中。

最佳答案

在 CP/M 时代,文件大小不是以字节为单位记录的,而是以 128 字节的 BLOCKS 记录。因此,有必要分配一个 END-OF-FILE 字节 (^Z=1AH) 来指示部分填充的文本文件 block (无 EOF 意味着“完全填充最后 128 字节 block ”)

MSDOS 及其衍生版本遵循此约定 - 这就是为什么 COPY CON: filename 以 ^Z 终止的原因。

COPY 命令允许对 ^Z 进行不同的解释 - 因此 /a/b 模式,如以下代码所示:

@ECHO OFF
SETLOCAL
:: set COPYCMD=Y to auto-allow overwriting of destination
SET copycmd=Y
:: copy using FRED1.txt (no ^Z-terminal)
>NUL COPY /a fred1.txt freda1.txt
>NUL COPY fred1.txt fred1a.txt /a
>NUL COPY /b fred1.txt fredb1.txt
>NUL COPY fred1.txt fred1b.txt /b
>NUL COPY /a fred1.txt freda1a.txt /a
>NUL COPY /a fred1.txt freda1b.txt /b
>NUL COPY /b fred1.txt fredb1a.txt /a
>NUL COPY /b fred1.txt fredb1b.txt /b

DIR fred*1*|FIND /i "fred"
ECHO.

:: copy using FRED2.txt (has ^Z-terminal) to fred*2*
>NUL COPY /a fred1.txt fred2.txt
>NUL COPY /a fred2.txt freda2.txt
>NUL COPY fred2.txt fred2a.txt /a
>NUL COPY /b fred2.txt fredb2.txt
>NUL COPY fred2.txt fred2b.txt /b
>NUL COPY /a fred2.txt freda2a.txt /a
>NUL COPY /a fred2.txt freda2b.txt /b
>NUL COPY /b fred2.txt fredb2a.txt /a
>NUL COPY /b fred2.txt fredb2b.txt /b

DIR fred*2*|FIND /i "fred"
ECHO.

:: append-copy using FRED1.txt+FRED2.txt to fred*3*
>NUL COPY /a fred1.txt+fred2.txt freda3.txt
>NUL COPY fred1.txt+fred2.txt fred3a.txt /a
>NUL COPY /b fred1.txt+fred2.txt fredb3.txt
>NUL COPY fred1.txt+fred2.txt fred3b.txt /b
>NUL COPY /a fred1.txt+fred2.txt freda3a.txt /a
>NUL COPY /a fred1.txt+fred2.txt freda3b.txt /b
>NUL COPY /b fred1.txt+fred2.txt fredb3a.txt /a
>NUL COPY /b fred1.txt+fred2.txt fredb3b.txt /b

DIR fred*3*|FIND /i "fred"
ECHO.

:: append-copy using FRED2.txt+FRED2.txt to fred*4*
>NUL COPY /a fred2.txt+fred2.txt freda4.txt
>NUL COPY fred2.txt+fred2.txt fred4a.txt /a
>NUL COPY /b fred2.txt+fred2.txt fredb4.txt
>NUL COPY fred2.txt+fred2.txt fred4b.txt /b
>NUL COPY /a fred2.txt+fred2.txt freda4a.txt /a
>NUL COPY /a fred2.txt+fred2.txt freda4b.txt /b
>NUL COPY /b fred2.txt+fred2.txt fredb4a.txt /a
>NUL COPY /b fred2.txt+fred2.txt fredb4b.txt /b

DIR fred*4*|FIND /i "fred"
ECHO.

GOTO :EOF

运行结果:

26/07/2013  08:51                 7 fred1.txt
26/07/2013  08:51                 7 fred1a.txt
26/07/2013  08:51                 7 fred1b.txt
26/07/2013  08:51                 8 freda1.txt
26/07/2013  08:51                 8 freda1a.txt
26/07/2013  08:51                 7 freda1b.txt
26/07/2013  08:51                 7 fredb1.txt
26/07/2013  08:51                 7 fredb1a.txt
26/07/2013  08:51                 7 fredb1b.txt

26/07/2013  08:51                 8 fred2.txt
26/07/2013  08:51                 8 fred2a.txt
26/07/2013  08:51                 8 fred2b.txt
26/07/2013  08:51                 8 freda2.txt
26/07/2013  08:51                 8 freda2a.txt
26/07/2013  08:51                 7 freda2b.txt
26/07/2013  08:51                 8 fredb2.txt
26/07/2013  08:51                 8 fredb2a.txt
26/07/2013  08:51                 8 fredb2b.txt

26/07/2013  09:35                15 fred3a.txt
26/07/2013  09:35                14 fred3b.txt
26/07/2013  09:35                15 freda3.txt
26/07/2013  09:35                15 freda3a.txt
26/07/2013  09:35                14 freda3b.txt
26/07/2013  09:35                15 fredb3.txt
26/07/2013  09:35                16 fredb3a.txt
26/07/2013  09:35                15 fredb3b.txt

26/07/2013  09:35                15 fred4a.txt
26/07/2013  09:35                14 fred4b.txt
26/07/2013  09:35                15 freda4.txt
26/07/2013  09:35                15 freda4a.txt
26/07/2013  09:35                14 freda4b.txt
26/07/2013  09:35                16 fredb4.txt
26/07/2013  09:35                17 fredb4a.txt
26/07/2013  09:35                16 fredb4b.txt

请注意并置 /a/b 如何产生不同的结果,所有结果都带有 ^Z 的各种组合 - 包含甚至附加。

另请注意,如果源是二进制文件并且省略了 /b 开关,则简单的 COPY 可能会失败。我从未调查过它,但我发现复制 .MPG 时偶尔会发生这种情况。我怀疑如果 ^Z 出现在文件中任何字节 >=80H 之前,则该文件被假定为 ASCII,因此立即终止 - 但正如我所说,我还没有调查它.

关于linux - 如何使用dos批处理删除文件中的Ctrl-Z,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17852802/

相关文章:

linux - Bash、Keys 和 rSync

android - Linux:启动 Android SDK 或 Android Emulator

java - 在 Java 中运行批处理文件

excel - 如何在 Excel 中获取显示值而不是实际值?

java - 尝试复制 HashMap 存储时出错

ios - 使用 iOS 将文本复制到剪贴板

linux - 如果找到关键字,则删除定界符之间的模式

c++ - gdb 核心转储打开失败

batch-file - %%~dpa 的含义?

windows - 如何将具有不同扩展名的文件作为 BAT 文件运行