FileWithNoEmptyLines.txt to remove the blan...">

Delete last line from file using Powershell

I use

gc FileWithEmptyLines.txt | where {$_ -ne ""} > FileWithNoEmptyLines.txt 

to remove the blank lines that SSRS places at the bottom of my CSV.

However, the last line of data on it ends with CRLF (as shown in Notepad ++) - and this is not deleted, so technically an empty line remains at the bottom of the file.

Is there a way to remove this CRLF from the last line (and, of course, keep the data intact)?

+8
text powershell csv
source share
5 answers

If you already know that the very last thing in the file is the CRLF that you want to get rid of (and you also know the encoding), you can go to the quick route:

 $stream = [IO.File]::OpenWrite('foo.txt') $stream.SetLength($stream.Length - 2) $stream.Close() $stream.Dispose() 

This is truncating the file in place. It works without reading the entire file into memory (very nice if you have a very large file). It works for ASCII, Latin- * and UTF-8. This will not work for UTF-16 (in this case you will have to remove four bytes from the end).

You can enable additional verification that the last two bytes are really what you want to delete:

 $stream = [IO.File]::Open('foo.txt', [IO.FileMode]::Open) $stream.Position = $stream.Length - 2 $bytes = 0..1 | %{ $stream.ReadByte() } $compareBytes = 13,10 # CR,LF if ("$bytes" -eq "$compareBytes") { $stream.SetLength($stream.Length - 2) } $stream.Close() $stream.Dispose() 

Again, adapt if you use a different encoding, for example. for UTF-16 you need to compare with either 0,10,0,13 or 10,0,13,0 .

I agree, this is not very PowerShell-ey, but since I had to process the dump of the 700-MiB database, I am afraid to read potentially large files in memory completely;)

+9
source share

When you read a file using Get-Content , it passes each line down the pipe as a line. When an Out-File (essentially that > is an alias) receives these lines, it always adds a string terminator sequence. Try the following if the files are not too large:

 $text = [IO.File]::ReadAllText("c:\FileWithEmptyLinesAtEnd.txt") [IO.File]::WriteAllText("c:\FileWithEmptyLinesAtEnd.txt", $text.TrimEnd()) 

This is the file before:

 14> fhex .\FileWithEmptyLinesAtEnd.txt Address: 0 1 2 3 4 5 6 7 8 9 ABCDEF ASCII -------- ----------------------------------------------- ---------------- 00000000 73 65 72 76 65 72 31 2C 73 65 72 76 65 72 32 2E server1,server2. 00000010 64 6F 6D 61 69 6E 2E 6C 6F 63 61 6C 2C 73 65 72 domain.local,ser 00000020 76 65 72 33 0D 0A 20 20 20 20 20 20 ver3.. 

and after:

 19> fhex .\FileWithEmptyLinesAtEnd.txt Address: 0 1 2 3 4 5 6 7 8 9 ABCDEF ASCII -------- ----------------------------------------------- ---------------- 00000000 73 65 72 76 65 72 31 2C 73 65 72 76 65 72 32 2E server1,server2. 00000010 64 6F 6D 61 69 6E 2E 6C 6F 63 61 6C 2C 73 65 72 domain.local,ser 00000020 76 65 72 33 ver3 
+5
source share

I'm not sure how much this applies to the situation, but my google search to delete the last line of a text file brought me here, and the above examples / solutions did not work. This is the command I could use to get this to work:

 $file = "file.txt" Get-Content $file | Measure-Object -Line $a = (Get-Content $file | Measure-Object) (Get-Content $file) | ? {($a.count-1)-notcontains $_.ReadCount} | Set-Content $file 

If you are working with a large file, you can first transfer it to the temp file.

+2
source share

Try this, although it will remove ALL empty lines from your file.

 (Get-Content c:\FileWithEmptyLinesAtEnd.txt) | Where-Object {$_ -match '\S'} | Out-File c:\FileWithEmptyLinesAtEnd.txt 
0
source share

For the UCS-2 Little Endian file format, use this option:

 $stream = [IO.File]::Open($filename, [IO.FileMode]::Open) $stream.Position = $stream.Length - 4 $bytes = 0..3 | %{ $stream.ReadByte() } $compareBytes = 13,0,10,0 # CR,LF echo "bytes: "$bytes if ("$bytes" -eq "$compareBytes") { $stream.SetLength($stream.Length - 4) } $stream.Close() $stream.Dispose() 
0
source share

All Articles