You can use the Get-Content
cmdlet for this and read lines until it finds the string you choose as starting point for this:
$filename = 'FULL PATH TO THE TOO LARGE TO OPEN FILE'
$outputPath = 'FULL PATH TO THE OUTPUT.TXT FILE'
$saveit = $false
Get-Content -Path $filename | ForEach-Object {
# the $_ automatic variable represents a single line of the file
if ($saveit) {
Add-Content -Path $outputPath -Value $_
}
else {
$saveit = ($_ -match 'DATA\s+FROM\s+NSERCH=\s+249')
}
}
The code below does the exact same thing, but requires .NET 4.0 or higher, so if you are using PowerShell 3.0 or up, you can use the [System.IO.File]::ReadLines()
method to speed things up:
$filename = 'FULL PATH TO THE TOO LARGE TO OPEN FILE'
$outputPath = 'FULL PATH TO THE OUTPUT.TXT FILE'
$saveit = $false
foreach ($line in [System.IO.File]::ReadLines($filename)) {
if ($saveit) {
Add-Content -Path $outputPath -Value $line
}
else {
$saveit = ($line -match 'DATA\s+FROM\s+NSERCH=\s+249')
}
}
Another Get-Content
alternative could be:
$filename = 'FULL PATH TO THE TOO LARGE TO OPEN FILE'
$outputPath = 'FULL PATH TO THE OUTPUT.TXT FILE'
$saveit = $false
$reader = [System.IO.File]::OpenText($filename)
while (!($reader.EndOfStream)) {
$line = $reader.ReadLine()
if ($saveit) {
Add-Content -Path $outputPath -Value $line
}
else {
$saveit = ($line -match 'DATA\s+FROM\s+NSERCH=\s+249')
}
}
$reader.Close()