Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - head and tail in PS?

Reply
 
Old 04-05-2007   #1 (permalink)
Renugopal


 
 

head and tail in PS?

I want to know whether head and tail command support present in PS not?
if present, hwta is syntax to use head & tail ?
Thanks
Renu

My System SpecsSystem Spec
Old 04-05-2007   #2 (permalink)
Xs3hell


 
 

Re: head and tail in PS?

type my.txt | select-object -first 10

type my.txt | select-object -last 10



"Renugopal" <Renugopal@discussions.microsoft.com> wrote in message
news:3C316884-7471-4755-82C2-14106048FD89@microsoft.com...
>I want to know whether head and tail command support present in PS not?
> if present, hwta is syntax to use head & tail ?
> Thanks
> Renu



My System SpecsSystem Spec
Old 04-05-2007   #3 (permalink)
forestial


 
 

Re: head and tail in PS?

This works OK but it is very slow on large files. 'select-object -first 10'
quickly displays the first 10 lines but then it sits there eating up the CPU
while the get-content cmdlet eats its way - uselessly - through the rest of
the file, and you can't cancel it.

Also I don't think I have seen a good way to provide the equivalent of 'tail
-f' where it will keep watching the contents of a log file or something that
is changing.

"Xs3hell" wrote:

> type my.txt | select-object -first 10
>
> type my.txt | select-object -last 10
>
>
>
> "Renugopal" <Renugopal@discussions.microsoft.com> wrote in message
> news:3C316884-7471-4755-82C2-14106048FD89@microsoft.com...
> >I want to know whether head and tail command support present in PS not?
> > if present, hwta is syntax to use head & tail ?
> > Thanks
> > Renu

>
>
>

My System SpecsSystem Spec
Old 04-05-2007   #4 (permalink)
Keith Hill


 
 

Re: head and tail in PS?

"forestial" <forestial@discussions.microsoft.com> wrote in message
news:6675F462-9D7D-4EB7-8999-F2418790BD6B@microsoft.com...
> This works OK but it is very slow on large files. 'select-object -first
> 10'
> quickly displays the first 10 lines but then it sits there eating up the
> CPU
> while the get-content cmdlet eats its way - uselessly - through the rest
> of
> the file, and you can't cancel it.


I think select -first and -last are handy for picking off objects in the
pipeline but as you say it -last isn't optimal for large files. I think we
need a cmdlet a bit more optimized for grabbing the last n lines from a
file.

> Also I don't think I have seen a good way to provide the equivalent of
> 'tail
> -f' where it will keep watching the contents of a log file or something
> that
> is changing.


Try Get-Content -wait log.txt

--
Keith

My System SpecsSystem Spec
Old 04-05-2007   #5 (permalink)
Xsh3ll


 
 

Re: head and tail in PS?

I can't find -wait in help, but it works? Is this an undocumented feature?
If yes then how did you found it?

>> Also I don't think I have seen a good way to provide the equivalent of
>> 'tail
>> -f' where it will keep watching the contents of a log file or something
>> that
>> is changing.

>
> Try Get-Content -wait log.txt
>





"Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
news:B39633B4-C38F-418E-BB3F-C2B2B19FCA45@microsoft.com...
> "forestial" <forestial@discussions.microsoft.com> wrote in message
> news:6675F462-9D7D-4EB7-8999-F2418790BD6B@microsoft.com...
>> This works OK but it is very slow on large files. 'select-object -first
>> 10'
>> quickly displays the first 10 lines but then it sits there eating up the
>> CPU
>> while the get-content cmdlet eats its way - uselessly - through the rest
>> of
>> the file, and you can't cancel it.

>
> I think select -first and -last are handy for picking off objects in the
> pipeline but as you say it -last isn't optimal for large files. I think
> we need a cmdlet a bit more optimized for grabbing the last n lines from a
> file.
>
>> Also I don't think I have seen a good way to provide the equivalent of
>> 'tail
>> -f' where it will keep watching the contents of a log file or something
>> that
>> is changing.

>
> Try Get-Content -wait log.txt
>
> --
> Keith


My System SpecsSystem Spec
Old 04-05-2007   #6 (permalink)
Keith Hill [MVP]


 
 

Re: head and tail in PS?

"Xsh3ll" <xshell@gmail.com> wrote in message
news:ebFoy55dHHA.4308@TK2MSFTNGP02.phx.gbl...
>I can't find -wait in help, but it works? Is this an undocumented feature?
>If yes then how did you found it?
>
>>> Also I don't think I have seen a good way to provide the equivalent of
>>> 'tail
>>> -f' where it will keep watching the contents of a log file or something
>>> that
>>> is changing.

>>
>> Try Get-Content -wait log.txt


I can't remember who showed me this trick but the deal is this. Right now
it seems that the docs aren't always accurate on the parameters that a
cmdlet accepts. However since everything in PowerShell is an object
including cmdlet definitions, you can always get the definitive parameter
list by doing this:

372# Get-Command Get-Command | fl


Name : Get-Command
CommandType : Cmdlet
Definition : Get-Command [[-ArgumentList] <Object[]>] [-Verb
<String[]>] [-Noun <String[]>] [-PSSnapin <String[]>
] [-TotalCount <Int32>] [-Syntax] [-Verbose] [-Debug]
[-ErrorAction <ActionPreference>] [-ErrorVaria
ble <String>] [-OutVariable <String>] [-OutBuffer
<Int32>]
Get-Command [[-Name] <String[]>] [[-ArgumentList]
<Object[]>] [-CommandType <CommandTypes>] [-TotalC
ount <Int32>] [-Syntax] [-Verbose] [-Debug] [-ErrorAction
<ActionPreference>] [-ErrorVariable <Strin
g>] [-OutVariable <String>] [-OutBuffer <Int32>]
<snip>

HTH,
Keith


My System SpecsSystem Spec
Old 04-05-2007   #7 (permalink)
Duncan Smith


 
 

Re: head and tail in PS?


>
> Try Get-Content -wait log.txt
>
> --
> Keith


Doesn't quite work as well as tail -f, if I run 'gc -wait test.txt'
when file contains "line1`n" and then enter 'aaa' save the file, enter
'bbb' save it and then 'ccc' save it (all without CR) then the output
from gc inserts CRs which are not there. so:

tail -f test.txt gives:

line1
aaabbbccc

which is correct, yet gc -wait test.txt gives:

line1
aaa
bbb
ccc

which is not quite right...

Regards,

Duncan.




My System SpecsSystem Spec
Old 04-05-2007   #8 (permalink)
Jacques Barathon [MS]


 
 

Re: head and tail in PS?

"forestial" <forestial@discussions.microsoft.com> wrote in message
news:6675F462-9D7D-4EB7-8999-F2418790BD6B@microsoft.com...
> This works OK but it is very slow on large files. 'select-object -first
> 10'
> quickly displays the first 10 lines but then it sits there eating up the
> CPU
> while the get-content cmdlet eats its way - uselessly - through the rest
> of
> the file, and you can't cancel it.


I am surprised that you can't cancel it. Are you trying with CTRL+C? I just
tried the above on a 800,000 line file and after displaying the first 10
lines I can press CTRL+C and return to the prompt immediately.

Anyway, when you want to retrieve the first x lines there is a very fast
alternative:

get-content largefile.txt -total x

The above will only read the first x lines.

When it comes to retrieving the tail of a large file it is a bit more
complex. You might be able to improve the overall performance by using the
System.IO .NET classes and methods to read your file. It would require some
experiment and testing though.

Jacques

My System SpecsSystem Spec
Old 04-05-2007   #9 (permalink)
Keith Hill


 
 

Re: head and tail in PS?

"Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
news:%23tl6xW8dHHA.596@TK2MSFTNGP06.phx.gbl...
> "forestial" <forestial@discussions.microsoft.com> wrote in message
> news:6675F462-9D7D-4EB7-8999-F2418790BD6B@microsoft.com...
>> This works OK but it is very slow on large files. 'select-object -first
>> 10'
>> quickly displays the first 10 lines but then it sits there eating up the
>> CPU
>> while the get-content cmdlet eats its way - uselessly - through the rest
>> of
>> the file, and you can't cancel it.

>
> I am surprised that you can't cancel it. Are you trying with CTRL+C? I
> just tried the above on a 800,000 line file and after displaying the first
> 10 lines I can press CTRL+C and return to the prompt immediately.
>
> Anyway, when you want to retrieve the first x lines there is a very fast
> alternative:
>
> get-content largefile.txt -total x
>
> The above will only read the first x lines.
>
> When it comes to retrieving the tail of a large file it is a bit more
> complex.


It seems like that complexity should be rolled into Get-Content and accessed
via a -last parameter. It would be nicer IMO if -total were renamed (or
aliased) to -first. It would be more like the Select-Object cmdlet but much
more efficient when dealing with huge log files.

--
Keith

My System SpecsSystem Spec
Old 04-05-2007   #10 (permalink)
Vinicius Canto


 
 

Re: head and tail in PS?

In article <3C316884-7471-4755-82C2-14106048FD89@microsoft.com>,
Renugopal@discussions.microsoft.com says...
> I want to know whether head and tail command support present in PS not?
> if present, hwta is syntax to use head & tail ?
> Thanks
> Renu
>


I have a workaround. you can download a Win32 version of Tail command
here:

http://tailforwin32.sourceforge.net/

--
"A maior glória não é ficar de pé, mas levantar-se cada vez que se cai."
Confúcio

Vinicius Canto
MVP Visual Developer - Scripting
MCP Windows 2000 Server, Windows XP e SQL Server 2000
Blog: http://viniciuscanto.blogspot.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Tail Logfile with WMI VB Script
tail -f file | cut ...? PowerShell
Tail functionality? PowerShell
Re: grep, which, and tail commands? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46