This is a more complex task requiring "state" management within the script. You want to keep the "Owner" line, then remove all lines that follow it, until you hit a line containing "Address" (which itself should also be removed). This process repeats. If an "Owner" line is found but no "Address" line follows before the end of the file, all subsequent lines will be removed.
Here's a PowerShell script to achieve this:
Sample data.log (make sure you have this file):
code
Start of record 1
Name: John Doe
Owner: SystemAdmin
Description: This is a system administrator record.
Notes: Important details here.
Address: 123 Main St, Anytown
End of record 1
Start of record 2
Name: Jane Smith
Status: Active
Contact: jane@example.com
Address: 500 Oak Ave, Somewhere
End of record 2
Start of record 3
Name: Service Account
Owner: IT_Dept
Role: Automation User
Last Login: 2023-10-26
Status: Locked
End of record 3
Another Section
Final Line
# Define the file you want to process
$filePath = ".\data.log"
$outputFilePath = ".\data_processed.log" # Output to a new file to prevent accidental data loss
# If you want to overwrite the original file, set: $outputFilePath = $filePath
# --- Pre-check: Verify file existence ---
if (-not (Test-Path $filePath)) {
Write-Error "Error: File '$filePath' not found."
exit 1
}
Write-Host "--- Starting processing of '$filePath' ---" -ForegroundColor Cyan
# Array to store the lines we want to keep
$linesToKeep = @()
# State variable: True if we are currently in a block that needs to be cut
$cuttingBlock = $false
# Read the file line by line
Get-Content $filePath | ForEach-Object {
$currentLine = $_
if ($cuttingBlock) {
# We are currently in a cutting block
if ($currentLine -like "*Address*") {
# Found the "Address" line. Stop cutting. This line itself is also cut.
$cuttingBlock = $false
Write-Host " -> Cutting block ended by 'Address' line." -ForegroundColor DarkYellow
} else {
# Still cutting lines within the block. Do not add to $linesToKeep.
Write-Host " -> Cutting line: '$currentLine'" -ForegroundColor Yellow
}
} else {
# We are NOT in a cutting block. Keep lines by default.
if ($currentLine -like "Owner*") {
# Found an "Owner" line. Keep this line, and then start cutting subsequent lines.
$linesToKeep += $currentLine
$cuttingBlock = $true # Set flag to start cutting from the *next* line
Write-Host " -> Found 'Owner' line: '$currentLine'. Starting to cut subsequent lines." -ForegroundColor Green
} else {
# Regular line, not "Owner", not in a cutting block. Keep it.
$linesToKeep += $currentLine
Write-Host " -> Keeping line: '$currentLine'" -ForegroundColor DarkGray
}
}
}
# --- Write the processed content to the output file ---
try {
$linesToKeep | Set-Content $outputFilePath -Encoding UTF8 # Using UTF8 is generally a good practice
Write-Host "`n--- Processing complete! ---" -ForegroundColor Cyan
Write-Host "Processed content saved to '$outputFilePath'." -ForegroundColor Green
} catch {
Write-Error "Error writing to file '$outputFilePath': $_"
}
# --- Display the processed file content (optional) ---
Write-Host "`n--- Content of '$outputFilePath': ---" -ForegroundColor Cyan
Get-Content $outputFilePath
How the script works:
$filePath: Specifies the input file.
$outputFilePath: Specifies where to save the modified content. It's highly recommended to use a new file first (data_processed.log) to verify the output before potentially overwriting your original data.log. If you are confident, you can change $outputFilePath to $filePath.
$linesToKeep = @(): An array that will collect all the lines that should not be cut.
$cuttingBlock = $false: This is our "state" variable.
It's false initially, meaning we are not currently in a block that needs cutting.
It becomes true when an "Owner" line is encountered.
It becomes false again when an "Address" line is encountered (or if the file ends while still in a cutting block).
Get-Content $filePath | ForEach-Object { ... }: Reads the file line by line and processes each one.
Logic inside ForEach-Object:
If $cuttingBlock is true:
This means we are currently in a section that needs to be cut.
If the $currentLine contains "Address", we set $cuttingBlock to false (stop cutting) and do not add this "Address" line to $linesToKeep (effectively cutting it).
If the $currentLine does not contain "Address", we just do nothing with it, which means it's also effectively cut (not added to $linesToKeep).
If $cuttingBlock is false:
This means we are currently in a section where lines should be kept by default.
If the $currentLine starts with "Owner", we add it to $linesToKeep, and then set $cuttingBlock to true. This ensures that subsequent lines will be cut.
If the $currentLine is neither "Owner" nor "Address" (and we're not cutting), we simply add it to $linesToKeep.
$linesToKeep | Set-Content ...: After processing all lines, the collected lines are written to the output file.
How to run the script:
Save the code above as a .ps1 file (e.g., Process-OwnerAddressBlocks.ps1).
Make sure your data.log file is in the same directory as the script, or provide the full path to data.log.
Open PowerShell.
Navigate to the directory where you saved the script.
Run the script:
powershell
.\Process-OwnerAddressBlocks.ps1
Expected Output (data_processed.log):
code
Start of record 1
Name: John Doe
Owner: SystemAdmin
End of record 1
Start of record 2
Name: Jane Smith
Status: Active
Contact: jane@example.com
End of record 2
Start of record 3
Name: Service Account
Owner: IT_Dept
Notice that for the "Service Account" block, since there was no "Address" line after "Owner", all subsequent lines ("Role", "Last Login", "Status", "End of record 3", "Another Section", "Final Line") were cut until the end of the file. This aligns with the "repeat to end of file" if no "Address" is found.