Remove specific content of lines in .txt file without having to write new file

So say i have example.txt, and it contains this:

Hello:World

World:Hello

How are you?:Good

Is there any way i could get rid of the middle line (only the section containing ‘world’) without needing to write an entire new file?

An example of my code so far that fully works, but writes another file to actually put the results in:

$files = gci -file *.txt # Get all files ending with .txt in the directory this is executing in, put them in an array named files

$a = 0 # Set variable to 0 (Currently a string!!!)
$n = [int]$a # convert it to a string in the 'N' variable, just to be sure

foreach($file in $files){ # For every file in this array, do the following
foreach($line in Get-Content $files[$n]){ # For every line in each file, do the following

New-Item .\$n.txt # Create a new file for each file it processes

Add-Content .\$n.txt $line.split(':')[1] # Write to this new file every loop

$n+1 # Increment 'n'

}
}

Any ideas? I can’t load the entire list into memory (As the actual data is much bigger then this example), and i would really like to avoid writing a new file as it would pretty much just waste time…

I don’t think it’s technically possible to avoid loading the file into memory. However, you can replace text in the file without loading each individual line into a variable, using -replace.

$files = gci -file *.txt # Get all files ending with .txt in the directory this is executing in, put them in an array named files

$a = 0 # Set variable to 0 (Currently a string!!!)
$n = [int]$a # convert it to a string in the 'N' variable, just to be sure

foreach($file in $files){ # For every file in this array, do the following

    $content = Get-Content -Path $file # Load the contents of the current file
    $content -replace 'World','Universe' | Set-Content -Path $file # Replace the target text in the contents, then write the output to the file

}

$content is a temporary holder for the text in each file as it gets processed through the foreach loop. Note that in line 9 when -replace executes it writes its change to standard output but not to the $content variable. You could store the output in another variable if you need to operate on it further, but for what you’ve asked the simplest thing to do is immediately pipe the output to Set-Content and overwrite the file.

You can use regex with -replace for more complex matching.

Not aware of a method to not open a file into memory to modify it. Not sure how large the files are, but here are multiple methods to open and read the files:

http://www.happysysadm.com/2014/10/reading-large-text-files-with-powershell.html

The basic idea is still do load the file into memory, process it and either overwriting the file processed or generating a new file.