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

Welcome Forums General PowerShell Q&A Remove specific content of lines in .txt file without having to write new file

Viewing 2 reply threads
  • Author
    • #256943
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

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

      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:

      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 1 week, 3 days ago by grokkit. Reason: code formatting
    • #257033
      Senior Moderator
      Topics: 10
      Replies: 168
      Points: 899
      Helping Hand
      Rank: Major Contributor

      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.

      $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.

    • #257045
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      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:

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

Viewing 2 reply threads
  • You must be logged in to reply to this topic.