How to concatenate corresponding lines in two text files

谁说我不能喝 提交于 2019-12-01 14:12:32

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!