Reputation: 25
How to call specific rows from file through PowerShell
out.csv
file.
I want to divide from 1 to 1000...
And I want to arrange this in the order 1,2,3,4...
Can it be done simply?
Get-Content "out.csv" | Select-Object -Index (1..1000) |set-content out_1.csv
Get-Content "out.csv" | Select-Object -Index (1001..2000) |set-content out_2.csv
Get-Content "out.csv" | Select-Object -Index (2001..3000) |set-content out_3.csv
Get-Content "out.csv" | Select-Object -Index (3001..4000) |set-content out_4.csv
Get-Content "out.csv" | Select-Object -Index (4001..5000) |set-content out_5.csv
Get-Content "out.csv" | Select-Object -Index (5001..6000) |set-content out_6.csv
Get-Content "out.csv" | Select-Object -Index (6001..7000) |set-content out_7.csv
Get-Content "out.csv" | Select-Object -Index (7001..8000) |set-content out_8.csv
Get-Content "out.csv" | Select-Object -Index (8001..9000) |set-content out_9.csv
Get-Content "out.csv" | Select-Object -Index (9001..10000) |set-content out_10.csv
Get-Content "out.csv" | Select-Object -Index (10001..11000) |set-content out_11.csv
Get-Content "out.csv" | Select-Object -Index (11001..12000) |set-content out_12.csv
Get-Content "out.csv" | Select-Object -Index (12001..13000) |set-content out_13.csv
Get-Content "out.csv" | Select-Object -Index (13001..14000) |set-content out_14.csv
Get-Content "out.csv" | Select-Object -Index (14001..15000) |set-content out_15.csv
Get-Content "out.csv" | Select-Object -Index (15001..16000) |set-content out_16.csv
Get-Content "out.csv" | Select-Object -Index (16001..17000) |set-content out_17.csv
I wrote it, but this seems too ignorant.
How many lines of script can it be expressed?.
.
The goal is to go into another script.
Preparations for multiple runs.
And my next question...
I need to run a separate list ip through powershell.
I'm thinking of opening several new windows.
I heard that distributed processing like that is faster.
Running it as one process takes a long time.
It would be nice if you could advise on this method as well.
thank you.
The end goal I'm going to put the file here.
Each file will run as a process with its own window, i.e. multiple processes.
This is done to speed up processing.
I'm still imagining I'm a beginner, so I don't know the commands, so I need to find out.
$ports=get-content \out_1.csv
Foreach($port in $ports){
New-NetFirewallRule -DisplayName 'A HTTP-Inbound' -Profile any -Direction Inbound -Action Block -RemoteAddress $port | Out-Null
}
out.txt = out.csv
Upvotes: 1
Views: 68
Reputation: 437833
To split a text file into chunks (number of lines) of a given size, using output files that contain a padded sequence number, use the following:
.txt
as the filename extension to make it clear that simple plain-text processing occurs; CSV files (.csv
) would require more complex processing, because only the first output file would include the header row (containing the column names) with this approach, as Santiago Squarzon points out.# Split input file out.txt into files out_01.txt, out_02.txt, ...
# with each output file containing (at most) 1000 lines.
$ndx = 0
Get-Content out.txt -ReadCount 1000 | ForEach-Object {
Set-Content -LiteralPath ('out_{0:00}.txt' -f ++$ndx) -Value $_
}
Note the use of Get-Content
's -ReadCount
parameter, which makes it read the input file into arrays of lines of the given size.
Upvotes: 5