批处理:从文本文件末尾删除换行符?

发布于 2024-11-13 17:51:34 字数 8 浏览 10 评论 0原文

continue

I have a .txt file where I need to get rid of the last line feed.
Looking at the file in a HEX Editor it shows "0d 0a" at the end.

I have looked at the thread How to delete Linefeed using batch file but that did not help.
I have tried COPY source target /b which also does not help.

Unfortunately I can't use Java or any third party tools, I need to use a batch file.

How can I get rid of that line feed at the end?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-11-20 17:51:34

使用批处理应该可以。

@echo off
setlocal DisableDelayedExpansion
set "firstLineReady="
(
    for /F "eol=$ delims=" %%a in (myFile.txt) DO (
        if defined firstLineReady (echo()
        set "firstLineReady=1"
        <nul set /p "=%%a"
    )
) > out.txt

它复制所有行并向每一行附加 CR/LF,但不附加到最后一行

Using batch this should work.

@echo off
setlocal DisableDelayedExpansion
set "firstLineReady="
(
    for /F "eol=$ delims=" %%a in (myFile.txt) DO (
        if defined firstLineReady (echo()
        set "firstLineReady=1"
        <nul set /p "=%%a"
    )
) > out.txt

It copies all lines and append to each line a CR/LF, but not to the last one

青萝楚歌 2024-11-20 17:51:34

尝试以下代码作为起点

@echo off
copy %1 temp.txt
echo d >debug.tmp
echo r >>debug.tmp
echo a >>debug.tmp
echo dec cx >>debug.tmp
echo dec cx >>debug.tmp
echo. >>debug.tmp
echo g =100 102 >>debug.tmp
echo w >>debug.tmp
echo q >>debug.tmp
debug temp.txt <debug.tmp

此批处理首先将文件复制到需要具有 8.3 名称的临时文件。

然后它准备一个调试脚本来截掉临时文件的最后两个字节。

前两条调试指令RD仅显示文件和寄存器的内容(其中重要的CX值包含文件的长度)它们可以被删除。

然后,调试脚本进入汇编程序模式 A 并生成两条 DEC CX 指令,将 CX 的值递减两倍。空行离开汇编模式。

该脚本执行 G 两条汇编指令。

然后,调试脚本将 W 写入与读取的相同内容的文件,但长度减去两个字节。最后退出Q调试。

此代码仅适用于小于 64kB 的文件。对于更大的文件,您需要扩展汇编代码,在递减 CX 递减 BX 后检查进位标志。

有关详细信息,请阅读 DEBUG /?,然后尝试 DEBUG?

Try the following code as an starting point

@echo off
copy %1 temp.txt
echo d >debug.tmp
echo r >>debug.tmp
echo a >>debug.tmp
echo dec cx >>debug.tmp
echo dec cx >>debug.tmp
echo. >>debug.tmp
echo g =100 102 >>debug.tmp
echo w >>debug.tmp
echo q >>debug.tmp
debug temp.txt <debug.tmp

This batch, first, copies the file to a temporary file that needs to have a 8.3 name.

Then it prepares a debug script to chop off the last two bytes of the temporary file.

The first two debug instructions R and D are only to show the contents of the file and the register (with the important CX value that contains the length of the file) They can be removed.

Then the debug script enters assembler mode A and produces two DEC CX instructions, that decrement twice the value of CX. The blank line leaves assembler mode.

The script executes G the two assembly instructions.

Then the debug script writes W back to the file the same contents read, minus two bytes in length. And finally quits Q debug.

This code only works with files smaller than 64kB. For bigger files you need to extend the assembly code, checking for carry flag after decrementing CX to decrement BX.

For more information read DEBUG /? and then try DEBUG and ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文