Friday, February 8, 2013

Powershell Script DIY: Remove Empty Lines From a Text File

Today we are going to learn to remove empty lines from a text file.   This can be useful if you need the input to be formatted a certain way for input into another script, or you just want your text file to look pretty.
First we start off with our location, and as with my other posts I set it at “C:\temp”:

Set-location “c:\temp”

Next we define our variables as:

$foo = "foo.txt"
$foolog = “foo.log

If you’re new to the field, Foo is a catchall for examples.  It can be literally anything.  Moving on we need to use the select-string command with the – pattern variable this tells Powershell that we are looking for a specific pattern in the file.

Select-String –Pattern

We are not even close to done as we need to define what we are looking for, which is anything.  This is done by using \w.  \w is used to find a word character, which is most anything (Except a line break):

Select-String -Pattern "\w"

So far we have said “Hey Powershell, Look for anything.” But as with everything in life, Powershell will respond with “Where?”.  This is where we tell it to look in our source file, or $foo:

Select-String -Pattern "\w" $foo

Now selecting the string its self is useless. It’s tantamount to saying “Hey You, Pick one!” and not following it up with anything.  So we use the pipe operator “|” to tell it that we want it to do something with it:

Select-String -Pattern "\w" $foo |

As there are multiple lines in our file, we need to use ForEach-Object to make Powershell recognize this:

Select-String -Pattern "\w" $foo | ForEach-Object

The next part is encapsulated in curly brackets “{}”.  You always have to encapsulate something when you are giving something a repeating command like “ForEach-Object”:

Select-String -Pattern "\w" $foo | ForEach-Object {}

Inside the Curley Brackets we need to tell it to go line by line through the file starting with the first line.  You can do this by saying start at the first line “$_” and do it for every subsequent line “.line”:

Select-String -Pattern "\w" $foo | ForEach-Object { $_.line}

Remember, This has to be in the brackets.  And finally, we dump the output into Foo.log, which is defined as $foolog. As this is an additional command we will use another | operator to add a “Set-Content” to the end:

Select-String -Pattern "\w" $foo | ForEach-Object { $_.line
} | Set-Content $foolog

In the end it should look something like this:

Set-Location “C:\temp”
$foo = "foo.txt"
$foolog = “foo.log
Select-String -Pattern "\w" $foo | ForEach-Object { $_.line
} | Set-Content $foolog

So to summarize, we:

1.       Set our location
2.       Defined our variables
3.       Selected any word character within foo.txt
4.       Told it to do this for every line
5.       Set the content to foo.log

Sounds like you should still have the lines unless you remember that we are selecting every “Word Character.”  Empty lines don’t count as characters so they are not extracted from the original file.
As always, please post your comments below.  I welcome the Critique and would love to hear if you have a different way of doing this!

Source: ScriptingGuy1

No comments:

Post a Comment