Tags


Powershell

I caught this one in Twitter yesterday.

“Is there a way to remove Blank Lines from the output?”

Top of my head I could think of an –EXCLUDE option for blanks but it isn’t hard to identify and remove blank lines from Text content.

So today’s task class is we’re going to use Powershell to erase Nothing.

“Huh?!?”

Well actually what we’re going to do is get rid of Blank lines from text files.

It’s pretty easy really.

When you read in the Content from GET-CONTENT it actually ends up as an Array of text,  meaning you can select and examine each line individually, including the number of characters.

So for the million Dollars, Dinner with Bill Gates, a signed autograph from Steve Ballmer with love *AND* a pat on the back from Paul Allen…… guess how many characters in a blank line?

None, 0, Zilch, Nada.  The CR/LF (Carriage Return line feed is used as a Delimiter and does not remain in the Array)

So when we do this

$FileContent=GET-CONTENT C:SOMESILLYTEXTFILE.TXT

$FileContent is actually an array.  Running a GET-MEMBER against it like this

$FileContent | GET-MEMBER

Will show us several properties including one important one called Length.  Length tells us how many members in that array (or in human speak, “lines”)

To access individual pieces of the array I execute this piece of code

For the first line

$FileContent[0]

For the second line

$FileContent[1]

For the third line

$FileContent[2]

Seeing a pattern emerge?  Arrays start counting at zero.   Knowing that is important because although the Length of the array might be 30 lines, you’ll stop at 29 for the last one (since you start at 0 not 1)

On any one of those Array members you can also run a GET-MEMBER like this

$FileContent[1] | GET-MEMBER

andf view its available Properties.  Each line ALSO has a Length property.   This is how many characters in a line.   So to identify blank lines we simply look for Members which have a zero length.

IF ($FileContent[1].Length –eq 0) { WRITE-HOST “Blank Line”}

But in our case we’re going to skip those lines and build a new array of text.  Easily done, we’ll create a variable called $NewArray typed as an [Array]

So we get the Size of the FileContent array first and bump back by 1 (Since our last position of the Array to be access is the Length of the Array –1, we started at 0 not 1 remember?)

$EndOfArray=($FileContent.Length)-1

Then we setup a new Blank array to store the data in

[Array]$NewArray=$NULL

Finally we run through the list with a Foreach loop, checking and returning lines that AREN’T blank, storing the built result into $NewArray.

FOREACH ($Position in 1..$End) {
     IF ($FileContent[1].Length –ne 0) { [Array]$NewArray+=$Filecontent[$Position])
}

Now as a script we would have something like this

——————————

$FileContent=GET-CONTENT C:Somesillyfile.txt

$EndOfArray=($FileContent.Length)-1

[Array]$NewArray=$NULL

FOREACH ($Position in 1..$End) {
     IF ($FileContent[1].Length –ne 0) { [Array]$NewArray+=$Filecontent[$Position])
}

——————————-

And we would have a simple script to pull out the and display it without Blank lines.   Here’s the fun part, we want this useful.    We can make this a function to add to our modules and “Return” the results back to the user.   One easy change, watch the Before and After

——————————

Function Global:REMOVE-BLANK ($FileContent) {

     $EndOfArray=($FileContent.Length)-1

     [Array]$NewArray=$NULL

     FOREACH ($Position in 1..$End) {
          IF ($FileContent[1].Length –ne 0) { [Array]$NewArray+=$Filecontent[$Position])
     }
Return $NewArray
}

——————————-

 

There you go!  Once defined in your profile or modules you can execute this code

$MyStuff=GET-CONTENT C:Somesillyfile.txt

$NoBlank=REMOVE-BLANK $MyStuff

You can now do whatever you what with your “Blank Free” data.

 

See?  That wasn’t so hard!  The Power of Shell is in YOU, now use it! 🙂

Sean
The Energized Tech

Advertisements