Powershell 在文件中四处移动行
Powershell move lines around in a file
这看起来很简单我不好意思问。
我有一个行数未知但至少有 20 行的文件。我总是想将第 4-6 行移动到第 1-3 行。
所以这个:
A
B
C
D
E
F
G
H
变成这样:
D
E
F
A
B
C
G
H
$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
$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
}
这看起来很简单我不好意思问。
我有一个行数未知但至少有 20 行的文件。我总是想将第 4-6 行移动到第 1-3 行。
所以这个:
A
B
C
D
E
F
G
H
变成这样:
D
E
F
A
B
C
G
H
$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
$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
}