Often we reach to a situation where we need to get top 10 or 100 lines of a file.
These commands are fairly simple in Unix shell programming and most of you must have used Head and Tail commands. Those commands work really fast and accurate.
Keeping these points in mind, I dig something and want to share will all of you.
How to use Head and Tail Commands in Powershell?
To get the top 10 lines -
Get-Content ".\file_test.txt" | select -First 10
To get the top 10 lines -
Get-Content ".\file_test.txt" | select -Last 10
Easy! This is power of Powershell !
Some more Examples:
Problem: Write a command to get 3rd line of a file.
Solution:
Get-Content ".\file_test.txt" | select -First 3 | select -Last 1
Problem: Write a command to skip 10 lines from top and display rest all lines of file.
Solution:
Get-Content ".\file_test.txt" | select -Skip 10
But this approach has one drawback, when file is so big, it takes a little more time to produce results. I am digging more and more to get this done with another approach, keep checking this article..
Enjoy Scripting!!
These commands are fairly simple in Unix shell programming and most of you must have used Head and Tail commands. Those commands work really fast and accurate.
Keeping these points in mind, I dig something and want to share will all of you.
How to use Head and Tail Commands in Powershell?
To get the top 10 lines -
Get-Content ".\file_test.txt" | select -First 10
To get the top 10 lines -
Get-Content ".\file_test.txt" | select -Last 10
Easy! This is power of Powershell !
Some more Examples:
Problem: Write a command to get 3rd line of a file.
Solution:
Get-Content ".\file_test.txt" | select -First 3 | select -Last 1
Problem: Write a command to skip 10 lines from top and display rest all lines of file.
Solution:
Get-Content ".\file_test.txt" | select -Skip 10
But this approach has one drawback, when file is so big, it takes a little more time to produce results. I am digging more and more to get this done with another approach, keep checking this article..
Enjoy Scripting!!