0

I'm using Powershell to trim spaces between strings, I need help. I'm reading the values into a variable using Get-Content

Here is my input data:

04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q
[ARMADA MUSIC]

12:37 Chakra - Home (Alexander Popov Remix) http://goo.gl/3janGY
[SOUNDPIERCING]

See the space between the two songs? I want to eliminate these. so that the output is:

04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q
[ARMADA MUSIC]
12:37 Chakra - Home (Alexander Popov Remix) http://goo.gl/3janGY
[SOUNDPIERCING] 
  • Eliminate one of the carriage return when two are found back-to-back. – PlamZ Jan 15 '15 at 15:52
  • @PlamZ - How would I write it? –  Jan 15 '15 at 15:52
  • possible duplicate of [remove empty lines from text file with PowerShell](http://stackoverflow.com/questions/9223460/remove-empty-lines-from-text-file-with-powershell) – Matt Jan 15 '15 at 15:54
  • I am not very competent syntax wise, but be sure to iterate through the text buffer and validate when two carriage return are found in two adjacent cells. Once you've done that, just apply an offset to the rest of the buffer and start again, in case there would be more than one. – PlamZ Jan 15 '15 at 15:54

2 Answers2

0

I put the contents in a file called foo.txt.

foreach ($line in get-content foo.txt) {
   if ($line -ne '') {
      $line
   }
}
Ben Thul
  • 31,080
  • 4
  • 45
  • 68
  • Thanks, it's not madatory but, could I do this with trim? I'm just wondering. –  Jan 15 '15 at 15:55
  • I don't know... you should try it for yourself and see what happens! The cost of failure is pretty low here. – Ben Thul Jan 15 '15 at 15:58
0
$noEmptyLines = Get-Content -Path C:\FilePath\File.Txt | Where-Object{$_ -notmatch "^\s*$"}

You would have a variable where any lines that contained only whitespace would be removed.

Matt
  • 45,022
  • 8
  • 78
  • 119