How to do what head, tail, more, less, sed do in Powershell? [closed]
On windows, using Powershell, what are the equivalent commands to linux's head
, tail
, more
, less
and sed
?
Get-Content
(alias: gc
) is your usual option for reading a text file. You can then filter further:
gc log.txt | select -first 10 # head
gc -TotalCount 10 log.txt # also head
gc log.txt | select -last 10 # tail
gc -Tail 10 log.txt # also tail (since PSv3), also much faster than above option
gc log.txt | more # or less if you have it installed
gc log.txt | %{ $_ -replace '\d+', '($0)' } # sed
This works well enough for small files, larger ones (more than a few MiB) are probably a bit slow.
The PowerShell Community Extensions include some cmdlets for specialised file stuff (e.g. Get-FileTail).
Here are the built-in ways to do head
and tail
. Don't use pipes because if you have a large file, it will be extremely slow. Using these built-in options will be extremely fast even for huge files.
gc log.txt -head 10
gc log.txt -tail 10
gc log.txt -tail 10 -wait # equivalent to tail -f