Can Powershell Read Lines Previous Lines Terminal
Text files are ubiquitous and you lot'll have to read through them, regardless of your part or chore responsibilities. In fact, as an It professional, you will have to create, read, or work with text files more often than you may retrieve considering text files come in a broad range of formats. This flexibility has besides made text files the nearly convenient mode to ascertain scripts, store configuration details, and more. PowerShell understands this importance of text files and makes it like shooting fish in a barrel to retrieve text and read from them easily. In this article, nosotros'll run into how you lot can read a text file in PowerShell.
Reading the entire contents
Shutterstock
When you want to read the entire contents of a text file, the easiest way is to use the built-in Get-Content role.
Here is the code that allows you lot to do this:
Get-Content C:\logs\log01012020.txt
When you execute this command, the contents of this file will be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute it.
You tin too move all the content to a variable and use that variable for further processing if that'south something that yous want your lawmaking to do.
$file_data = Get-Content C:\logs\log01012020.txt
You can now use this variable $file_data for parsing or further processing.
Reading partial content
Many times, you may simply have to read a certain function of a file to get the information yous desire. Similar to a SQL query, you can choose the lines you want to read and the code for that is:
$file_data = Get-Content C:\logs\log01012020.txt $file_data | Select-Object -First 10
Every bit the code suggests, the start 10 lines of the content will be stored in the variable and non the entire content. You can display the contents of this variable or use information technology for more processing.
Also, we can read the concluding few lines too:
$file_data = Become-Content C:\logs\log01012020.txt $file_data | Select-Object -Last x
Reading line-past-line
When you want to read the file to understand its contents, you lot'd have to do so one line at a time and the expert news is, this is possible with PowerShell.
In fact, at that place are two ways to do information technology.
Using Get-Content
The Get-Content function reads every line in the text and stores them as an array, where each line is an array element. In the above example, we used a variable for reading all the content.
$file_data = Become-Content C:\logs\log01012020.txt
If yous print the type of this variable, you can see that it an array.
This means you can choice out specific lines using the assortment index. For case, if you want to read the starting time line, you can simply say:
$file_data[0]
And this will display the outset line for y'all.
$file_data[1]
This will display the second line, and and so on.
Using StreamReader form
The 2d selection is to utilise a .NET form called StreamReader.
$stream_reader = New-Object Arrangement.IO.StreamReader{C:\logs\log01012020.txt}
Now you lot have the contents of the log file in this $stream_reader variable and since it belongs to the StreamReader class, you tin can utilise many built-in methods to go the text you want.
When y'all say…
$stream_reader.ReadToEnd()
…this will output all the contents on your screen, like to Get-Content.
To read the current line, y'all can use the post-obit method:
$stream_reader.ReadLine()
But this alone may not be helpful, then y'all'll accept to run information technology in a loop and read the contents of a text file, line by line.
$stream_reader = New-Object System.IO.StreamReader{C:\logs\log01012020.txt} $line_number = 1 while (($current_line =$stream_reader.ReadLine()) -ne $goose egg) { Write-Host "$line_number $current_line" $line_number++ } The above code snippet volition get-go with the starting time line and will impress out each line with its line number for like shooting fish in a barrel reading. You can even use any of the available cord methods on the $current_line variable to parse information technology further.
This method is platonic for reading large files because when yous shop the contents in a variable, information technology can accept up too much memory and tin can impact performance as well. And so, this is a more efficient way to get to the content you lot want.
Finding specific text
Many times, you'd want to detect a specific text in a file and the all-time to do that is to use the Where-Object cmdlet that filters the content to give you what you lot want.
$file_data = Go-Content C:\logs\log01012020.txt | Where-Object {$_ -like '*mistake*'}
The to a higher place lawmaking will output those lines that have the word "error" in them. Hither, the $_ is a pipeline variable that represents the electric current line from the contents that come up from Get-Content.
Likewise Where-Object, you can also employ match and regex operators to detect the verbal text you want.
More options with Become-Content
Become-Content is a highly versatile cmdlet that comes loaded with many options. Here are some things you lot can practice with it.
Count the number of lines in the file
You may want to know the number of lines available in the file, and then you can parse information technology accordingly. The code for that is:
$file_data = Go-Content C:\logs\log01012020.txt | Measure-Object
Specific number of lines at the commencement and the stop
Before, we saw how to choose the offset few or concluding few lines using the Select-Object cmdlet. You can also become the aforementioned results using some of the congenital-in methods of Get-Content.
To get the outset few lines, the method is:
$file_data = Get-Content C:\logs\log01012020.txt -TotalCount 3
This will return the first three lines from the file.
$file_data = Become-Content C:\logs\log01012020.txt -Tail iii
This command will render the concluding three lines from the file.
Continuously updated file
Let'south say your log file is continuously updated and you desire to meet the cease of the log file to read the last updated values. For that, you tin can add the Look parameter, like this:
$file_data = Get-Content C:\logs\log01012020.txt -Tail 3 -Wait
This command will continuously monitor the log file for the new lines that are getting added to it and will brandish the same to y'all.
Thus, these are some of the different ways to read a text file in PowerShell. Practise let us know if you know more means to read the contents of a file in PowerShell.
Featured image: Freerange Stock
More than PowerShell Nuts articles
- How to Check Your Windows Server Uptime with PowerShell
- How to Uninstall Software Using PowerShell
- Do Y'all Like Your PowerShell Wet or Dry?
- Scripting help from the Windows Admin Center
- Getting Started with PowerShell for Microsoft Teams
Source: https://techgenix.com/read-text-file-powershell/
0 Response to "Can Powershell Read Lines Previous Lines Terminal"
Postar um comentário