Powershell 在文件中 move 行

标签 powershell line move

这看起来很简单,我都不好意思问。

我的文件行数未知,但至少有 20 行。我总是想将第 4-6 行移至第 1-3 行。

所以这个:

A

B

C

D

E

F

G

H

变成这样:

D

E

F

A

B

C

G

H

最初的尝试不起作用。

$path = "C:\xxxx"
$pathnew = $path + "\new"
If(!(test-path $pathnew))
{
      New-Item -ItemType Directory -Force -Path $pathnew
}


[System.Collections.ArrayList]$Files = @(Get-ChildItem $path -Filter *.json |
    Where-Object {$_.PSIsContainer -eq $false} | 
    Select-Object FullName,Name)

foreach ($File in $Files) {
    [System.Collections.ArrayList]$File_Contents = @(Get-Content $File.FullName)

    $FN = $File.FullName 
    $OutPutFile = $pathnew + "\" + $File.Name 

    $txt = Get-Content $FN
    $txt2 = Get-Content $FN | Measure-Object -Line 
    $txtLess3 = $txt2.Lines-3
    $txt[6..12],"`"submission`": {",$txt[1..3]
    ,"  `"performanceYear`": 2019},"
    ,$txt[13..$txtLess3] | Out-File $OutPutFile 
}

这不起作用

最佳答案

$Lines = 'A'..'H'
$Lines[3..5],$Lines[0..2],$Lines[6..99]

$Lines[3,4,5,0,1,2,6,7,8,9]

详情请参见:about arrays

关于Powershell 在文件中 move 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60772541/

相关文章:

C++ 不允许转换为右值引用

powershell - 在 Azure 云服务上将防恶意软件设置为默认启用

powershell - 打破 Azure 工作流 Runbook 中的 ForEach 循环

C# 画线 OnPaint() 与 CreateGraphics()

objective-c - 就像在 Interface Builder 中一样在窗口之间画线

Python,行迭代器

c++ - lambda 的初始化顺序作为接受其他参数的参数

c++ - move 语义、标准集合和构造时间地址

powershell - 确定 Powershell 中不同时区的夏令时状态

powershell - 在PowerShell脚本中,是否可以判断是否使用默认参数值?