One slick trick you can do in PowerShell with text files that's difficult to accomplish without extra tools is to display sections of text files without showing the entire file. When you use the Get-Content cmdlet, the resulting content is treated as an array of strings. This means you can simply specify the line you want displayed like this:
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
(get-content c:\boot.ini)[0]
Don't forget that arrays start counting at 0. If you want to see a range of lines, simply specify the range:
(get-content c:\boot.ini)[0..5]
This will display the first 6 lines of c:\boot.ini. Want to look at the last X number of lines? Well you first need to get the number of lines.
(get-content c:\boot.ini).Length
Once you know the length, specify whatever range you want. My boot.ini is 7 lines long so if I wanted to see the last 3 lines I would simply run
(get-content c:\boot.ini)[5..7]
Of course, this little tidbit wouldn't be complete without a one-liner to get the last 5 lines without knowing in advance how long the file is:
$c=get-content c:\boot.ini;$l=$c.length;$c[($l-5)..$l]
If you specify a number greater than the value of $l then the entire file will be returned.
Obviously the boot.ini file is hardly an exciting thing to test so try this out on log files on your systems.
These commands are very similar to the Head and Tail utilities and in fact you could easily turn these into your own Head and Tail functions.
Hi,
I have posted another solution (and some comments) about the last command, to show only the last 5 lines of a file…
Instead of
$c=get-content c:\boot.ini;$l=$c.length;$c[($l-5)..$l]
you can use
(cat c:\boot.ini)[-5..-1]
The only problem is… my blog is in Portuguese. There are few content about Powershell and scripting in my country…
If you have some time:
http://viniciuscanto.blogspot.com/2007/02/tail-x-get-content-obtendo-as-ltimas.html
Tks!
—
Vinicius Canto < scripterbr_at_gmail_dot_com >
MVP Visual Developer – Scripting
MCP Windows 2000 Server, Windows XP e SQL Server 2000
Blog sobre Scripting: http://viniciuscanto.blogspot.com
Using the alias, cat, certainly makes it easier. Just as a point of reference, I try to avoid using aliases so that people understand what cmdlet is really doing the work.
Your comment certainly works if you know the length of the file. I also should have pointed out that you can use:
cat c:\boot.ini -totalcount 3
and that will return the first 3 lines. -1 means return everything so you can use that to get the end of the file. But certainly a handy HEAD subsitute.
Yeah, great…
But my recommendation works even when you don’t know de lenght of the file. If you use an invalid interval (like use [-4000..-1] with a file with only four lines, the command still works.
By curiosity, I found on the Internet the legend, the original Tail command, but for Windows!
http://tailforwin32.sourceforge.net/
I haven’t tested yet, but I liked.
Congratulations for you blog… it’s very good. It’s in my blog list now!
Vinicius
I must have had fumbled fingers the first time I tried your suggestion because it didn’t work for me. Maybe I missed the minus signs. But I see now what you are talking about.
Thanks.
Unlike tail, which I believe reads the file in reverse n number of lines, using [-n..-m] or | select -last n seems to require PS to read the entire file and then just return what you’ve selected. Try using either of the proposed PS techniques to “tail” a 300 MB file. It takes five minutes, compared to 5 seconds with tail.
For a tail equivalent, PS would need to filter the input, not the output.
I'm just trying to find Linux equivalents for PS to work on our big log files on Windows and its true, the PS commands are taking about 3 minutes to get a result back which is not acceptable for me. Hope that there is another solution to read the file from the end instead of reading the whole file.
Any ideas yet?
Thanks.
You might wait and try using the version of Get-Content in PowerShell v2.0. Performance with large text files is always an issue.
I just ran
PS C:\windows> (gc .\WindowsUpdate.log)[-10..-1]
In PowerShell v2 on a log file with 16511 lines and it took hardly any time at all to grab the last 10 lines.