CMD 或 Powershell 命令组合(合并)来自两个文件的相应行 [英] CMD or Powershell command to combine (merge) corresponding lines from two files

查看:31
本文介绍了CMD 或 Powershell 命令组合(合并)来自两个文件的相应行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 CMD 和 Powershell 将 2 个文件合并为 1 个文件,如下所示:

<前>file1-line1 选项卡 file2-line1file1-line2 选项卡 file2-line2file1-line3 选项卡 file2-line3

所以它需要文件 1 的第 1 行并插入一个制表符,然后插入文件 2 的第 1 行.然后对每个文件中的所有后续行执行此操作吗?

解决方案

在 PowerShell 中,并假设两个文件的行数完全相同:

$f1 = Get-Content file1$f2 = 获取内容文件 2for ($i = 0; $i -lt $f1.Length; ++$i) {$f1[$i] + "`t" + $f2[$i]}

Is it possible using CMD and Powershell to combine 2 files into 1 file like this:

file1-line1 tab file2-line1
file1-line2 tab file2-line2
file1-line3 tab file2-line3

So it takes file 1 line 1 and the inserts a tab and then inserts file 2 line 1. Then does this for all subsequent lines in each file?

解决方案

In PowerShell, and assuming both files have exactly the same number of lines:

$f1 = Get-Content file1
$f2 = Get-Content file2

for ($i = 0; $i -lt $f1.Length; ++$i) {
  $f1[$i] + "`t" + $f2[$i]
}

这篇关于CMD 或 Powershell 命令组合(合并)来自两个文件的相应行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆