Search This Blog

Wednesday, February 18, 2015

#45 : Display top n lines or last n lines of a file

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.

To be able to read a big file, you can adjust Get-Content to read only few lines. As you saw, we were reading the complete file before that caused more memory consumption.



Enjoy Scripting!!

11 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Even better. But still last 5 lines might take longer time for large files.

    ReplyDelete
  3. to get last lines of large files use:
    get-content $myFile -tail 5

    ReplyDelete
  4. - tail rockz thx !

    ReplyDelete
  5. Top 5: get-content $targetFile -totalcount 5

    Don't pipe if you don't have to!

    ReplyDelete
  6. These are the examples for extracting the data from a file, but i want extract data from command output, so how can i achieve this?

    ReplyDelete
    Replies
    1. Simply put your data into a variable, then pipe that into the select command.

      For example:
      $data = get-process
      Then $data | select -last 10

      Delete
  7. Is there any way to search between two occurrences of a word.

    ReplyDelete
  8. I am trying to pull the last few lines from huge files and this seems to take forever... I'm still waiting (minutes) for the first attempt to work. I would love the -tail option but it does not seem to work in my powershell. At least the pipe to select works:

    PS C:\temp> Get-Content .\remapp_printers.txt | select -last 3
    \\mhmv-qs-100v\277R100HPCLJ4525DN - 1a
    \mhmv-qs-10 = plk-fs-01 - 1b
    New default printer = \\mhmv-qs-100v\15 PCL6 - 12/5/2016 - 6
    PS C:\temp> Get-Content .\remapp_printers.txt -tail 3
    Get-Content : A parameter cannot be found that matches parameter name 'tail'.
    At line:1 char:40
    + Get-Content .\remapp_printers.txt -tail <<<< 3
    + CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBin
    dingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
    ands.GetContentCommand

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  9. Hi Kevin and All,

    I was searching for some Live online help for Powershell.
    However i didn't find any suitable online live group

    So, i thought to create a Skype Group.
    Here we can help each other to solve any Powershell issue or puzzle.

    Please join me on https://join.skype.com/yY056Inw1A8q

    Let's help each other. we really appreciate your help

    Regards,
    Param

    ReplyDelete

#91 : How to export SQL Server table to JSON?

JSON stands for Javascript Object Natation. It has become a standard format to exchange and store data since when Big data revolution has be...