Remove line from a text file

Welcome Forums General PowerShell Q&A Remove line from a text file

  • This topic has 17 replies, 5 voices, and was last updated 12 months ago by
    js
    Participant
    .
Viewing 13 reply threads
  • Author
    Posts
    • #163875
      Participant
      Topics: 15
      Replies: 68
      Points: 306
      Rank: Contributor

      I have some text files. The files have five/six blank lines between two particular lines. I want to remove the extra blank line between those two lines and keep only one blank line. Any suggestions on how to achieve this.

       

    • #163887
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      What do you have so far? Please show your code.

    • #163914
      Participant
      Topics: 15
      Replies: 68
      Points: 306
      Rank: Contributor

      $test=Get-Content “C:\test.txt”

      if ($test[6] -match ‘\s*’)

      {

      $newfile = $test | Select-Object -Skip $test[6]

      }

      Initially, I was just trying to remove a empty line which I know exists at line 5. However this is not working also.

    • #163926
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      Hmmm … if the blank line is always at the same position you could simply use something like this:

      $Content = Get-Content -Path 'C:\_temp\testblank.txt' -OutVariable 
      $Content[0..5]
      $Content[7..$Content.Count]
    • #163935
      Participant
      Topics: 15
      Replies: 68
      Points: 306
      Rank: Contributor

      Actually I want to calculate the number of blank lines between two lines in a text file and then just keep one blank line and remove the others.

      Say the file is something like:

      This is line number 5

      This is line number 6

       

       

      This is line number n.

      I want to calculate the blank lines after the line “This is line number 6” and before the next line which contains some text and keep only one blank line and delete the other extra blank lines.

    • #163947
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      If these lines are really blank – even no additional whitespace charachters – you could do something like this determine the blank lines:

      Select-String -Path C:\test.txt -Pattern '^$' |
          Select-Object -Property LineNumber

      BTW: What’s the actual prupose of that task .. does it serve any advantage or benefit?

      • #164169
        Participant
        Topics: 15
        Replies: 68
        Points: 306
        Rank: Contributor

        This will output the blank lines. However I don’t want all the blank lines in the file.  There are some extra blank lines in the file after a particular line and I want to remove the extra blank lines and keep only a single blank line.

        There are multiple files like this and all files have slightly different content. The common thing is that there is a particular line say

        “This is line number 6” which is common in all files. So I just want to calculate the blank lines after a particular line and remove the extra blank lines, keeping only one blank line after this particular line.

    • #163950
      Participant
      Topics: 15
      Replies: 68
      Points: 306
      Rank: Contributor

      After determining the blank lines. How do I remove them the blank lines except one?

      • #163971
        Participant
        Topics: 5
        Replies: 2368
        Points: 5,987
        Helping Hand
        Rank: Community MVP

        You simply output the lines you want and skip the ones you don’t want.

    • #164019
      Senior Moderator
      Topics: 9
      Replies: 1236
      Points: 4,443
      Helping Hand
      Rank: Community Hero

      A logic on top of my head,

      1. Iterate through each line using foreach
      2. Save the current line content in a variable, say $Previous
      3. Add a logic to check if $Current.length = 0 And $Previous = $Current, then skip $Current, Else
      4. Output $Current
    • #164292
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      OK, let me try to bring this to an end. Using Prasoons idea you’d start with something like this to remove any double empty line:

      Get-Content -Path C:\test.txt |
          ForEach-Object {
          $PreviousLineEmpty = $CurrentLineEmpty
              if ($_ -notmatch '^$') {
                  $_
                  $CurrentLineEmpty = $false
              }
              else {
                  $CurrentLineEmpty = $true
                  if (-not ($PreviousLineEmpty -and $CurrentLineEmpty)) {
                      $_
                  }
              }
          }
      
      Remove-Variable PreviousLineEmpty, LastLineEmpty

      Next time we expect a little more action from you!! 😉 😀

    • #164497
      Participant
      Topics: 0
      Replies: 45
      Points: 243
      Helping Hand
      Rank: Participant

      as per above case, we can use below simple one line code to remove white  extra line from the text file.

      (Get-Content -Path C:\test.txt | Where-Object {$_ -ne ''}) | Out-File C:\test.txt
    • #164512
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      as per above case, we can use below simple one line code to remove white extra line from the text file.

      You know that was NOT the requirement, don’t you? 😉 😀

    • #164536
      Participant
      Topics: 0
      Replies: 45
      Points: 243
      Helping Hand
      Rank: Participant

      So we can modify it litter bit then 😉

      $files=(Get-Content -Path D:\log.txt)
      Clear-Content -path D:\log.txt
      $files | where-object {if($_ -ne ""){
      Write-output "$_"`n | Out-File d:\log.txt -Append
      }}
      • #164551
        Participant
        Topics: 5
        Replies: 2368
        Points: 5,987
        Helping Hand
        Rank: Community MVP

        The requirement was NOT to remove ALL empty lines. 😉 Your code does still the same.

      • #164557
        Participant
        Topics: 0
        Replies: 45
        Points: 243
        Helping Hand
        Rank: Participant

        Ok, what i am understanding  here is that, if we have text file where we have extra line.we need to remove that

        Let take an example:

        line 1

        space1

        space2>

        line 2

        space1

        space2

        space3

        <space5>

        line3

        so we need to make sure that contains of file, should be like this

        line 1

        space1

        line 2

        space1

        line3

        so, as per my logic, Yes it will remove all space first and make sure we have only one white space in between two line while copy the back to text file.so technically it is not removing all line from file.There is anything else, I am missing here .

    • #164659
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      so, as per my logic, Yes it will remove all space first and make sure we have only one white space in between two line while copy the back to text file.so technically it is not removing all line from file.There is anything else, I am missing here .

      If I got the requirement right: more than one empty lines should be reduced to just one empty line. So something like this:

      This is line number 1
      This is line number 2
      This is line number 3
      
      This is line number 5
      This is line number 6
      
      
      
      
      
      
      This is line number 13
      This is line number 14
      This is line number 15

      Should end up like this:

      This is line number 1
      This is line number 2
      This is line number 3
      
      This is line number 5
      This is line number 6
      
      This is line number 13
      This is line number 14
      This is line number 15

      Does you code do this? Try it please.

    • #164665
      js
      Participant
      Topics: 30
      Replies: 828
      Points: 2,554
      Helping Hand
      Rank: Community Hero

      Note that out-file changes the encoding to unicode by default.

Viewing 13 reply threads
  • The topic ‘Remove line from a text file’ is closed to new replies.