【问题标题】:Merge two txt files as columns in Windows在 Windows 中将两个 txt 文件合并为列
【发布时间】:2021-11-08 03:09:19
【问题描述】:

我需要能够合并为具有数百万行的单独列文件。我在这里尝试使用建议的代码:

@echo off
set f1=1.txt
set f2=2.txt
set outfile=mix.txt
type nul>%outfile%
(
    for /f "delims=" %%a in (%f1%) do (
        setlocal enabledelayedexpansion
        set /p line=
        echo(%%a!line!>>%outfile%
        endlocal
    )
)<%f2%

pause

Concatenate 2 txt files line by line using Batch

但是当我使用非 ASCII(希腊语)文本运行它时,我得到一个奇怪的输出,结果文件中的编码从 UTF-8 更改为 Windows-1253,并带有损坏的希腊字符(尽管涉及所有文件,包括批处理文件, 是 UTF-8).另外,我没有分隔符(我希望它是制表符)。

示例输入

文件1

Agenda

文件2

Διάταξη των εργασιών

输出

AgendaΔιάταξη των ΞµΟΞ³Ξ±ΟƒΞΉΟŽΞ½

期望的输出

Agenda[TAB]Διάταξη των εργασιών

【问题讨论】:

    标签: batch-file text-manipulation


    【解决方案1】:

    您需要使用代码页 65001

    @echo off
     CD /d "%~dp0"
     CHCP 65001 > nul
     Setlocal EnableExtensions DisableDelayedExpansion
    
     for /f "delims= " %%T in ('robocopy /L . . /njh /njs' )do set "TAB=%%T"
    
     set "f1=%~dp01.txt"
     set "f2=%~dp02.txt"
     set "outfile=%~dp0mix.txt"
     break>"%outfile%"
    
    <"%f2%" (
     for /f "usebackq delims=" %%a in ("%f1%") do (
      set /p "right="
      set^ "left=%%a"
      setlocal enabledelayedexpansion
      >>"%outfile%" (echo(!left!!TAB!!right!)
      endlocal
    ))
    
    type "%outfile%"
    Pause
    

    输出:

    Agenda  Διάταξη των εργασιών
    

    编辑

    批处理/powershell 解决方案,可处理高达 8191 字节的行

    @echo off
     CD /d "%~dp0"
     CHCP 65001 > nul
    
     set "f1=%~dp01.txt"
     set "f2=%~dp02.txt"
     set "outfile=%~dp0mix.txt"
     break>"%outfile%"
    
     >"%Outfile%" (
       powershell -noprofile -command ^
       "$File1 = @(Get-Content "%f1%" -Encoding utf8); $File2 = @(Get-Content "%f2%" -Encoding utf8);$Max = [math]::Max($File1.GetUpperBound(0), $File2.GetUpperBound(0)); for($i = 0; $i -le $Max; $i++) {write-host ($File1[$i],$File2[$i]) -Separator "`t"}"
      )
    
     type "%outfile%" > Con
     Pause
    goto:Eof
    

    注意 - 上述内容并不限定两个文件的行数相等,也不会改变如果一个字符串为空而另一个字符串不是则所采取的操作 - 这些是您的问题中未讨论的考虑因素,因此将不会在此答案中解决 - 请注意,如果您预计可能会遇到不相等数量的行,并且需要防止在此类行之前添加或附加分隔符,则应考虑这样做。

    细分:

    • 将文件加载到数组中,指定 utf8 编码:
    $File1 = @(Get-Content "%f1%" -Encoding utf8)
    
    $File2 = @(Get-Content "%f2%" -Encoding utf8)
    
    • 获取最大行数:
     $Max = [math]::Max($File1.GetUpperBound(0), $File2.GetUpperBound(0))
    
    • 对于从 0 到最大值的每个数组索引
      • 在指定分隔符的当前索引处写入行:
     for($i = 0; $i -le $Max; $i++) {
      write-host ($File1[$i],$File2[$i]) -Separator "`t"
     }
    

    最后,与 powershell .ps1 脚本相同:

     CHCP 65001 | Out-null
    
     $F1 = (resolve-path -path $PSScriptRoot\1.txt).Path
     $F2 = (resolve-path -path $PSScriptRoot\2.txt).Path
     $Outfile = "$PSScriptRoot\mix.txt"
    
     $File1 = @(Get-Content "$F1" -Encoding utf8)
     $File2 = @(Get-Content "$F2" -Encoding utf8)
    
     $Max = [math]::Max($File1.GetUpperBound(0), $File2.GetUpperBound(0))
    
     $(for($i = 0; $i -le $Max; $i++) {
      $line = ($File1[$i],$File2[$i]) -join "`t"
      Write-Output $Line
     }) | Out-File $Outfile -encoding utf8
    
     Type $Outfile
    

    【讨论】:

    • 有趣。当我用记事本/EmEditor 打开时,我仍然看到损坏的字符;当我使用 Notepad++ 打开时,我看到的是普通文本。选项卡未添加到输出文件中,如果行太长(大约超过 1300 字节),则会将其截断并与不同的行对齐。以下是测试和输出的文件:translatum.gr/downloads/test-batch.zip 例如,mix.txt 中的第 251 行的内容来自 1.txt 的第 251 行和来自 2.txt(希腊文本)的第 247 行(截断)。
    • 通过superuser.com/a/685264/747811 此处的建议修复了 UTF-8 问题,仍然存在断行和缺少标签的问题。
    • both 文件行太长? set /P 命令只能读取大约 1021 个字符,因此您必须通过 for / 读取最长的行文件,而使用 set /P 读取较短行的文件
    • 嗯,这些只是测试文件。真实世界的文件在源文件和目标文件中可能会超过 1021 个字符。不确定应该更改哪些代码以适应这种情况。
    • 是的,我的错。我不知道可能存在这样的问题。无论如何,只是尝试使用 cmd 的“编辑”版本:'CHCP' is not recognized as an internal or external command, operable program or batch file. 'powershell' is not recognized as an internal or external command, operable program or batch file. 它没有产生输出文件。
    猜你喜欢
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多