Remove line from a text file

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.

 

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

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

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]

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.

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?

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

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

A logic on top of my head,

    <li>Iterate through each line using foreach</li>
    
    <li>Save the current line content in a variable, say $Previous</li>
    
    <li>Add a logic to check if $Current.length = 0 And $Previous = $Current, then skip $Current, Else</li>
    
    <li>Output $Current</li>
    

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.

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!! :wink: :smiley:

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

You know that was NOT the requirement, don’t you? :wink: :smiley:

So we can modify it litter bit then :wink:

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

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

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 .

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.

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