Welcome › Forums › General PowerShell Q&A › Remove specific content of lines in .txt file without having to write new file
- This topic has 2 replies, 3 voices, and was last updated 4 months, 1 week ago by
Participant.
-
AuthorPosts
-
-
September 16, 2020 at 3:20 am #256943
So say i have example.txt, and it contains this:
PowerShell12345Hello:WorldWorld:HelloHow are you?:GoodIs 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:
PowerShell12345678910111213141516$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 sureforeach($file in $files){ # For every file in this array, do the followingforeach($line in Get-Content $files[$n]){ # For every line in each file, do the followingNew-Item .\$n.txt # Create a new file for each file it processesAdd-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…
-
This topic was modified 4 months, 1 week ago by
grokkit. Reason: code formatting
-
This topic was modified 4 months, 1 week ago by
-
September 16, 2020 at 11:13 am #257033
I can’t load the entire list into memory
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.
PowerShell1234567891011$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 sureforeach($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.
-
September 16, 2020 at 1:36 pm #257045
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.
-
-
AuthorPosts
- The topic ‘Remove specific content of lines in .txt file without having to write new file’ is closed to new replies.