这看起来很简单,我都不好意思问。
我的文件行数未知,但至少有 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/