Remove blank lines from command output stored in a variable

1

My variable output may have 100 lines. This is how it appears:

VOLUME #28
 drive letter        = I:
 dev_name            = \Device\HarddiskVolume8
 guid_name           = \\?\Volume{35596739-d159-11e4-80c1-0050569cf1fd}
 size                = 5117 MB.
 disk tag            = 0x000000304f65e304.
 descriptor          = \\.\Volume{35596739-d159-11e4-80c1-0050569cf1fd}

 **** ERROR: DATA NOT AVAILABLE [0x00000100] ****

VOLUME #28
 drive letter        = B:
 dev_name            = \Device\HarddiskVolume63
 guid_name           = \\?\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}
 size                = 81917 MB.
 disk tag            = 0x000000304f65e304.
 descriptor          = \\.\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}

 **** ERROR: DATA NOT AVAILABLE [0x00000100] ****

This is how I want it to look like:

VOLUME #29
 drive letter        = I:
 dev_name            = \Device\HarddiskVolume8
 guid_name           = \\?\Volume{35596739-d159-11e4-80c1-0050569cf1fd}
 size                = 5117 MB.
 disk tag            = 0x000000304f65e304.
 descriptor          = \\.\Volume{35596739-d159-11e4-80c1-0050569cf1fd}
 **** ERROR: DATA NOT AVAILABLE [0x00000100] ****
VOLUME #28
 drive letter        = B:
 dev_name            = \Device\HarddiskVolume63
 guid_name           = \\?\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}
 size                = 81917 MB.
 disk tag            = 0x000000304f65e304.
 descriptor          = \\.\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}
 **** ERROR: DATA NOT AVAILABLE [0x00000100] ****

I'm checking to see if there is a way to remove the blank lines in the variable. Thank you!

powershell
asked on Stack Overflow Oct 4, 2016 by jes • edited Oct 4, 2016 by wOxxOm

2 Answers

2

You can do that with a regex:

(?m)^\s*?\n

Where (?m) sets the multi line flag, ^ asserts position at start of a line, \s*? matches any whitespace character between zero and unlimited times (non greedy) and \n matches a linefeed.


Example:

$myVar = 
@'
VOLUME #28
 drive letter        = I:
 dev_name            = \Device\HarddiskVolume8
 guid_name           = \\?\Volume{35596739-d159-11e4-80c1-0050569cf1fd}
 size                = 5117 MB.
 disk tag            = 0x000000304f65e304.
 descriptor          = \\.\Volume{35596739-d159-11e4-80c1-0050569cf1fd}

 **** ERROR: DATA NOT AVAILABLE [0x00000100] ****

VOLUME #28
 drive letter        = B:
 dev_name            = \Device\HarddiskVolume63
 guid_name           = \\?\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}
 size                = 81917 MB.
 disk tag            = 0x000000304f65e304.
 descriptor          = \\.\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}

 **** ERROR: DATA NOT AVAILABLE [0x00000100] ****
'@

$myVar -replace '(?m)^\s*?\n'

Output:

VOLUME #28
 drive letter        = I:
 dev_name            = \Device\HarddiskVolume8
 guid_name           = \\?\Volume{35596739-d159-11e4-80c1-0050569cf1fd}
 size                = 5117 MB.
 disk tag            = 0x000000304f65e304.
 descriptor          = \\.\Volume{35596739-d159-11e4-80c1-0050569cf1fd}
 **** ERROR: DATA NOT AVAILABLE [0x00000100] ****
VOLUME #28
 drive letter        = B:
 dev_name            = \Device\HarddiskVolume63
 guid_name           = \\?\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}
 size                = 81917 MB.
 disk tag            = 0x000000304f65e304.
 descriptor          = \\.\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}
 **** ERROR: DATA NOT AVAILABLE [0x00000100] ****
answered on Stack Overflow Oct 4, 2016 by Martin Brandl • edited Oct 4, 2016 by Martin Brandl
0

Your variable is an output from external utility, so it's an array as you can see by checking its type: $output.GetType() will show System.Object[] which means an array.

Simply use an array comparison operator:

$noBlanks = $output -ne ''

Or regex that also filters out lines with just spaces and tabs:

$noBlanks = $output -notmatch '^\s*$'
answered on Stack Overflow Oct 4, 2016 by wOxxOm • edited Oct 4, 2016 by wOxxOm

User contributions licensed under CC BY-SA 3.0