|
|
PGTS Humble BlogThread: Perl Programming |
|
Gerry Patterson. The world's most humble blogger | |
Edited and endorsed by PGTS, Home of the world's most humble blogger | |
| |
Tail for windows 7 |
|
Chronogical Blog Entries: |
|
| |
Date: Mon, 29 Feb 2016 23:00:00 +1100Many things have been fixed in Windows 7. Things that really should have been fixed 20 years ago. Still there is no "tail" command. |
The Windows CMD shell works better than it ever has, and a number of commands have been added which make it easy to use. However there is still no official "tail" command.
For example, if you are one of those rare dinosaurs who is capable of typing faster than 40 words per minute and prefers to work at the command prompt, it might be convenient to have a "tail" command, if you wanted to quickly find out how much space is left on the share \\foobar\data\share ... If there were, you could find out with command such as the following:
C:\> dir \\foobar\data\share | tail -1
6 Dir(s) 150,123,200,768 bytes free
Or (another common scenario) if there was a large log file on a particular windows share, It would be handy to see the last 4 lines by using a command like the following:
C:\> tail -4 \\foobar\data\share\log\large_log.log
LOGENTRY 101000011 - foo bar blah blah the quick
LOGENTRY 101000012 - foo bar blah blah silver fox
LOGENTRY 101000013 - foo bar blah blah jumped over
LOGENTRY 101000014 - foo bar blah blah the lazy brown dog
Alas, there is no such command, and as the trend seems to be towards GUI interfaces, it is unlikely that there will be. There are several examples online, of work-arounds for this situation. I prefer to simply cut my own code. If you are a veteran command line user, you will have almost certainly installed perl on the Windows machine ... This will be either Strawberry or Activestate.
If so, the following little perl script will function effectively as a "tail" command (and/or pipe):
#!/usr/bin/perl my @buf; my $LINES = 10; if (@ARGV && $ARGV[0] =~ /^\-(\d+)$/) { $LINES = $1; die "zero lines" unless ($LINES > 0); shift @ARGV; } while (<>) { push @buf,$_; if (scalar(@buf) > $LINES) { shift @buf; } } foreach(@buf) {print}
Just save it in your path as "tail.pl" and it will work (Provided of course that you have run ASSOC and FTYPE with appropriate parameters and setup PATHEXT to run .pl scripts as if they were commands).
Note: the "-" option is kludged (rather than using Getopt::Std) because this way we can get the number that is often entered with a tail (or head) command ... The default value is "10" (like the Unix commands). This script will die if the value is zero ... Although you might prefer to simply exit (1).
BTW: This script is not as sophisticated as the Unix command. It will be slower and it does not check to see if the file is binary ... No doubt you could add a check for binary content if you wished (might want to add the "strict" and "warning" pragmas if you do). Also, if you are running this script on a Modern Windows 7 or Server 2008 machine it will be quick enough. Of course a "head" command would be trivial ... I'll leave it up to you to write your own.