How to concatenate corresponding lines in two text files

后端 未结 1 950
清酒与你
清酒与你 2021-01-17 01:52

I have two text files that I need to combine by concatenating line n from file #2 to line n of file #1 to make line n of file #3. They will always have the same number of l

1条回答
  •  时光说笑
    2021-01-17 02:40

    You can use a For loop to do this in PowerShell:

    $File1 = Get-Content C:\Path\To\File1.txt
    $File2 = Get-Content C:\Path\To\File2.txt
    $File3 = @()
    For($a=0;$a -lt $File1.count;$a++){
        $File3+=$File1[$a].trim()+$File2[$a].trim()
    }
    $File3 | Out-File C:\Path\To\NewFile.txt
    

    Edit: Ok, I didn't think of single line files. That would make $File1 and/or $File2 strings instead of arrays. We can make them arrays easily enough so it can properly index into them:

    $File1 = @((Get-Content C:\Path\To\File1.txt))
    $File2 = @((Get-Content C:\Path\To\File2.txt))
    $File3 = @()
    For($a=0;$a -lt $File1.count;$a++){
        $File3+=$File1[$a].trim()+$File2[$a].trim()
    }
    $File3 | Out-File C:\Path\To\NewFile.txt
    

    0 讨论(0)
提交回复
热议问题