如何使用批处理删除txt文件中的倒数第二行?

时间:2022-12-22 19:24:59

I've searched this site MANY times and found MANY related questions but none were exactly like my question so... I merely need to delete the 2nd to last line in a txt file (%txtfile%.txt) and that's all. The reason it's the 2nd to last is because the file was made using echo.Sample text>>"%txtfile%.txt" and so it left an emty line at the end. I now want to delete the last line EXCEPT for the blank line. How do I do this?

我搜索过这个网站很多次,发现很多相关的问题,但没有一个像我的问题那样......我只需要删除txt文件中的第二行到最后一行(%txtfile%.txt),这就是全部。它是倒数第二的原因是因为该文件是使用echo.Sample文本>>“%txtfile%.txt”制作的,因此它在结尾处留下了一个emty行。我现在想要删除空白行的最后一行EXCEPT。我该怎么做呢?

1 个解决方案

#1


0  

Your definition of last line is different then mine. The pair of characters <CarriageReturn><LineFeed> terminate a line. At the end of your file you have a line that is terminated, and nothing after it. If you open the file in a text editor and go to the end of the file, the cursor will be on an empty line. But that line is not part of the current file. In my mind, the last line is the line above.

你对最后一行的定义与我的不同。字符对 终止一行。在文件的末尾,您有一行已终止,后面没有任何内容。如果在文本编辑器中打开文件并转到文件末尾,则光标将位于空行。但该行不是当前文件的一部分。在我看来,最后一行是上面的一行。

You can Use FIND to determine the number of lines. FIND also does not consider there to be a line after the last line terminator. Then use FINDSTR to prefix each line with the line number piped to a second FINDSTR to filter out the "last" line. A FOR loop reads the output and SET search and replace removes the line number prefix. Delayed expansion is toggled on and off within the loop to protect against ! being corrupted if it is found within the content of the file during the expansion of the %%L variable.

您可以使用FIND确定行数。 FIND也不认为在最后一行终止符之后有一行。然后使用FINDSTR为每一行添加前缀为第二个FINDSTR的行号,以过滤掉“最后”行。 FOR循环读取输出,SET搜索和替换删除行号前缀。延迟扩展在循环内打开和关闭以防止!如果在扩展%% L变量期间在文件内容中找到它,则会被破坏。

@echo off
setlocal disableDelayedExpansion
set file="test.txt"
for /f %%N in ('find /c /v "" ^<%file%') do set skip=%%N
(
  for /f "delims=" %%L in ('findstr /n "^" %file%^|findstr /v "^%skip%:"') do (
    set "ln=%%L"
    setlocal enableDelayedExpansion
    echo(!ln:*:=!
    endlocal
  )
)>%file%.new
move %file%.new %file% >nul

After running the code above, the desired line will have been stripped out. The last line will still be terminated, so a text editor will still show an empty line after the end of the file.

运行上面的代码后,所需的行将被删除。最后一行仍将终止,因此文本编辑器在文件结束后仍会显示空行。

#1


0  

Your definition of last line is different then mine. The pair of characters <CarriageReturn><LineFeed> terminate a line. At the end of your file you have a line that is terminated, and nothing after it. If you open the file in a text editor and go to the end of the file, the cursor will be on an empty line. But that line is not part of the current file. In my mind, the last line is the line above.

你对最后一行的定义与我的不同。字符对 终止一行。在文件的末尾,您有一行已终止,后面没有任何内容。如果在文本编辑器中打开文件并转到文件末尾,则光标将位于空行。但该行不是当前文件的一部分。在我看来,最后一行是上面的一行。

You can Use FIND to determine the number of lines. FIND also does not consider there to be a line after the last line terminator. Then use FINDSTR to prefix each line with the line number piped to a second FINDSTR to filter out the "last" line. A FOR loop reads the output and SET search and replace removes the line number prefix. Delayed expansion is toggled on and off within the loop to protect against ! being corrupted if it is found within the content of the file during the expansion of the %%L variable.

您可以使用FIND确定行数。 FIND也不认为在最后一行终止符之后有一行。然后使用FINDSTR为每一行添加前缀为第二个FINDSTR的行号,以过滤掉“最后”行。 FOR循环读取输出,SET搜索和替换删除行号前缀。延迟扩展在循环内打开和关闭以防止!如果在扩展%% L变量期间在文件内容中找到它,则会被破坏。

@echo off
setlocal disableDelayedExpansion
set file="test.txt"
for /f %%N in ('find /c /v "" ^<%file%') do set skip=%%N
(
  for /f "delims=" %%L in ('findstr /n "^" %file%^|findstr /v "^%skip%:"') do (
    set "ln=%%L"
    setlocal enableDelayedExpansion
    echo(!ln:*:=!
    endlocal
  )
)>%file%.new
move %file%.new %file% >nul

After running the code above, the desired line will have been stripped out. The last line will still be terminated, so a text editor will still show an empty line after the end of the file.

运行上面的代码后,所需的行将被删除。最后一行仍将终止,因此文本编辑器在文件结束后仍会显示空行。