The way to learn a textual content file in PowerShell shortly and simply

Textual content information are ubiquitous and also you’ll need to learn via them, no matter your function or job duties. The truth is, as an IT skilled, you’ll have to create, learn, or work with textual content information extra typically than it’s possible you’ll assume as a result of textual content information are available a variety of codecs. This flexibility has additionally made textual content information probably the most handy option to outline scripts, retailer configuration particulars, and extra. PowerShell understands this significance of textual content information and makes it simple to retrieve textual content and skim from them simply. On this article, we’ll see how one can learn a textual content file in PowerShell.

Studying your complete contents

linux text editors

Shutterstock

Once you wish to learn your complete contents of a textual content file, the best means is to make use of the built-in Get-Content operate.

Right here is the code that means that you can do that:

Get-Content material C:logslog01012020.txt

Once you execute this command, the contents of this file will likely be displayed in your command immediate or the PowerShell ISE display, relying on the place you execute it.

You may as well transfer all of the content material to a variable and use that variable for additional processing if that’s one thing that you really want your code to do.

$file_data = Get-Content material C:logslog01012020.txt

Now you can use this variable $file_data for parsing or additional processing.

Studying partial content material

powershell-text-files

Many occasions, it’s possible you’ll solely need to learn a sure a part of a file to get the data you need. Just like a SQL question, you possibly can select the traces you wish to learn and the code for that’s:

$file_data = Get-Content material C:logslog01012020.txt
$file_data | Choose-Object -First 10

Because the code suggests, the primary 10 traces of the content material will likely be saved within the variable and never your complete content material. You possibly can show the contents of this variable or use it for extra processing.

Likewise, we are able to learn the previous few traces too:

$file_data = Get-Content material C:logslog01012020.txt
$file_data | Choose-Object -Final 10

Studying line-by-line

Once you wish to learn the file to know its contents, you’d have to take action one line at a time and the excellent news is, that is potential with PowerShell.

The truth is, there are two methods to do it.

Utilizing Get-Content material

The Get-Content material operate reads each line within the textual content and shops them as an array, the place every line is an array ingredient. Within the above instance, we used a variable for studying all of the content material.

$file_data = Get-Content material C:logslog01012020.txt

In case you print the kind of this variable, you possibly can see that it an array.

This implies you possibly can select particular traces utilizing the array index. For instance, if you wish to learn the primary line, you possibly can merely say:

$file_data[0]

And this may show the primary line for you.

$file_data[1]

This can show the second line, and so forth.

Utilizing StreamReader class

The second possibility is to make use of a .NET class referred to as StreamReader.

$stream_reader = New-Object System.IO.StreamReader

Now you might have the contents of the log file on this $stream_reader variable and because it belongs to the StreamReader class, you need to use many built-in strategies to get the textual content you need.

Once you say…

$stream_reader.ReadToEnd()

…this may output all of the contents in your display, much like Get-Content material.

To learn the present line, you need to use the next technique:

$stream_reader.ReadLine()

However this alone will not be useful, so that you’ll need to run it in a loop and skim the contents of a textual content file, line by line.

$stream_reader = New-Object System.IO.StreamReader
$line_number = 1
whereas (($current_line =$stream_reader.ReadLine()) -ne $null)

The above code snippet will begin with the primary line and can print out every line with its line quantity for straightforward studying. You possibly can even use any of the accessible string strategies on the $current_line variable to parse it additional.

This technique is right for studying giant information as a result of while you retailer the contents in a variable, it may take up an excessive amount of reminiscence and might impression efficiency as properly. So, it is a extra environment friendly option to get to the content material you need.

Discovering particular textual content

Many occasions, you’d wish to discover a particular textual content in a file and the very best to try this is to make use of the The place-Object cmdlet that filters the content material to present you what you need.

$file_data = Get-Content material C:logslog01012020.txt | The place-Object

The above code will output these traces which have the phrase “error” in them. Right here, the $_ is a pipeline variable that represents the present line from the contents that come from Get-Content material.

In addition to The place-Object, it’s also possible to use match and regex operators to seek out the precise textual content you need.

Extra choices with Get-Content material

Get-Content material is a extremely versatile cmdlet that comes loaded with many choices. Listed below are some issues you are able to do with it.

Depend the variety of traces within the file

It’s possible you’ll wish to know the variety of traces accessible within the file, so you possibly can parse it accordingly. The code for that’s:

$file_data = Get-Content material C:logslog01012020.txt | Measure-Object

Particular variety of traces in the beginning and the tip

Earlier, we noticed how to decide on the primary few or previous couple of traces utilizing the Choose-Object cmdlet. You may as well get the identical outcomes utilizing among the built-in strategies of Get-Content material.

To get the primary few traces, the strategy is:

$file_data = Get-Content material C:logslog01012020.txt -TotalCount 3

This can return the primary three traces from the file.

$file_data = Get-Content material C:logslog01012020.txt -Tail 3

This command will return the final three traces from the file.

Repeatedly up to date file

Let’s say your log file is constantly up to date and also you wish to see the tip of the log file to learn the final up to date values. For that, you possibly can add the Wait parameter, like this:

$file_data = Get-Content material C:logslog01012020.txt -Tail 3 -Wait

This command will constantly monitor the log file for the brand new traces which can be getting added to it and can show the identical to you.

Thus, these are among the other ways to learn a textual content file in PowerShell. Do tell us if you understand extra methods to learn the contents of a file in PowerShell.

Featured picture: Freerange Inventory


Publish Views:
2

Extra PowerShell Fundamentals articles






Learn Subsequent


About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *