【问题标题】:Insert line break after every command output in command prompt在命令提示符中的每个命令输出后插入换行符
【发布时间】:2022-01-17 07:36:10
【问题描述】:

我想使用命令提示符将每个文本文件的内容保存在另一个输出文本中。我用过这个命令:

type ".\test\*.txt" > out.txt

但是每个输出都附加在该文本文件中。如何在读取每个文本文件后在输出文件中引入换行符。

【问题讨论】:

  • 不清楚您想要什么,显示当前示例以及预期结果。添加新行很容易,但不确定您的文件是什么样的以及您期望的结果是什么。
  • 你在我发表评论几分钟后编辑了你的问题,却完全忽略了我的澄清要求?
  • 如果您的单个文件没有 CRLF 行结尾,那么您的合并结果文件将不会在每个文件之间有分隔符。如果您想在每个文件的开头和/或结尾添加额外的 CRLF,那么您的文件必须先包含这些文件,然后才能使用单个 type 命令执行此操作。在需要的地方引入它们的唯一方法是编写可以获取每个单独文件内容的代码,通过以结尾和/或开头“分隔符”的数量等方式读取和解析它,然后添加适当的数字那些需要的地方。这当然不是一个简单的命令任务。

标签: windows batch-file cmd eol


【解决方案1】:

你的问题有点含糊,所以我猜你只需要根据需要/想要添加尽可能多的回声。

这是对forfiles /? 给出的众多示例之一的轻微修改

forfiles /p test /m "*.txt" /C "cmd /c type @file >>../out.txt &&echo. >>../out.txt &&echo. >>../out.txt

确保在开始之前重命名或删除任何现有的 out.txt

【讨论】:

    【解决方案2】:

    有几种可能,我想和大家分享一下:

    1. 使用type按原样返回每个文件,然后使用findstr /V "$"找出最后一行是否由换行符终止,如果不是,则使用conditional execution operator &&显式输出echo/:

       @echo off
       rem // Write to output file:
       > "out.txt" (
           rem // Loop through input files to resolve wildcards:
           for %%F in (".\test\*.txt") do (
               rem // First type out the currently iterated file:
               type "%%~F"
               rem /* Let `findstr` detect whether there is a terminating line-break,
               rem    and conditionally append one if not: */
               > nul findstr /V /M "$" "%%~F" && echo/
           )
       )
      

      注意事项:

      • 文件大小必须小于 2 GiB?
    2. 使用find /V "",如果不存在,则在最后一行附加一个换行符。由于find 通常会输出我们不需要的标头,因此请通过input redirection < 而不是arguments 提供文件。由于重定向不支持wildcards,让standard for-loop 解决它们:

       @echo off
       rem // Write to output file:
       > "out.txt" (
           rem // Loop through input files since `<` does not support wildcards:
           for %%F in (".\test\*.txt") do (
               rem // Let `find` return all lines with a terminating line-break:
               < "%%~F" find /V ""
           )
       )
      

      注意事项:

      • 文件大小必须小于 2 GiB?
      • 行数必须小于 4 KiB;
    3. 使用more /V "",如果不存在,则将换行符附加到最后一行。为避免在每个文件后出现提示,请让 for-loop 解析 wildcards 而不是 more:

       @echo off
       rem // Write to output file:
       > "out.txt" (
           rem // Loop through input files since `more` awaits prompt per file on wildcards:
           for %%F in (".\test\*.txt") do (
               rem // Let `more` return all lines with a terminating line-break:
               more "%%~F"
           )
       )
      

      注意事项:

      • 文件大小必须小于 2 GiB?
      • 文件包含的 Ki 行数不得超过 64 行;
      • 行数必须小于 64 KiB;
      • TABs 扩展为 SPACEs;
    4. 使用sort,如果不存在,则在最后一行附加一个换行符。为避免重新排列行,请指定字符开始位置以超出支持的行长度。由于sort 然后反转整个文件,只需应用命令两次:

       @echo off
       rem // Write to output file:
       > "out.txt" (
           rem // Loop through input files to not unintentionally rearrange lines:
           for %%F in (".\test\*.txt") do (
               rem // Let `sort` return all lines with a terminating line-break:
               sort "%%~F" /+65535 /REC 65535 | sort /+65535 /REC 65535
           )
       )
      

      注意事项:

      • 文件大小必须小于 2 GiB?
      • 行数必须小于 64 KiB;
    5. 通过for /F读取每个文件并回显每一行,因此每个文件,即使是最后一个文件,都会被换行符终止; findstr 在每行前面加上行号加上一个冒号,以使它们不会在for /F 中显示为空,这将跳过这些行;然后前缀在循环体中被剥离:

       @echo off
       rem // Write to output file:
       > "out.txt" (
           rem // Loop through input files since `for /F` does not support wildcards:
           for %%F in (".\test\*.txt") do (
               rem // Precede each line by line number plus `:`, then read each augmented line:
               for /F "delims=" %%L in ('findstr /N "^" "%%~F"') do (
                   rem // Store currently read augmented line:
                   set "LINE=%%L"
                   rem // Toggle delayed expansion to aviod loss of `!`:
                   setlocal EnableDelayedExpansion
                   rem // Remove line number prefix and return current line with a line-break:
                   echo(!LINE:*:=!
                   endlocal
               )
           )
       )
      

      注意事项:

      • 文件大小必须小于 2 GiB;
      • 行必须短于大约 8 KiB;

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多