Friday, 20 February 2015

Display first n lines, lines in middle or last n lines of a file with powershell.

Display first n lines, lines in middle or last n lines of a file with powershell.

Sometimes we need some specific content for a file, may be a version number , config details that lies in a specific file or config file.

We will be using the PowerShell cmdlets to achieve this.

Create a file with 10 lines with 1..10 and name it as file.txt



Now we use the cmdlet Get-content and get the required output using select.

Getting first 5 lines


Getting last 5 lines


Skipping first 3 lines and displaying rest..

Display some line in the middle say line 5,



This could be a useful approach to get the data fast across infrastructure with the help of PS remoting.

3 comments:

  1. how to get the 5th row to 10th row

    ReplyDelete
  2. Awesome work, So I just want to share an online converter that will help you to convert one file into another like there;

    Simple File Converter

    ReplyDelete