[Chicago-talk] -i, -n and print - removing the top x lines w/ each run

Andy_Bach at wiwb.uscourts.gov Andy_Bach at wiwb.uscourts.gov
Thu Sep 27 12:16:42 PDT 2007


Hey.

Background:
Trying to fool the 'watch' utility into displaying a log file in it's 
--difference mode. So I wanted to write a script that'd display the first 
X lines of a file and remove them, saving the rest of the file, so each 
call of the script and file name shortens the data file from the top down. 
 I came up w/
#!/usr/bin/perl -i -n
if ( 1 .. /End/ ) {
   s/$/\f/ if $. == 1 ;
   warn($_);
}
else {
print;
}

so (the "\f" is for watche:
watch -n2 -d watch_ao_stats.pl /tmp/vmstats.txt

works it's way through the (copy) of the vmstats.txt file which has 
vmstats data, something like:
--- Start 09/27/07 13:15:08 CDT ------

--- vmstat 
procs                      memory      swap          io     system cpu
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy 
id wa
 2  0   5616 266224 169100 1296832    0    0     0     0    2     1  1  0 
1  0
--- End 09/27/07 13:15:21 CDT ------

--- Start 09/27/07 13:15:21 CDT ------

--- vmstat (5 runs)
procs                      memory      swap          io     system cpu
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy 
id wa
 1  0   5616 269444 169104 1296940    0    0     0     0    2     1  1  0 
1  0
--- End 09/27/07 13:15:35 CDT ------

It all sort of works, but using 'warn' there to get user output (and 
'print' to rewrite the in-place edited file) doesn't work w/ 'watch' - it 
mangles the output. Is there a way I can do the "truncate from the top w/ 
each run" trick?

a

Andy Bach
Systems Mangler
Internet: andy_bach at wiwb.uscourts.gov
VOICE: (608) 261-5738  FAX 264-5932

Nothing in life is certain except death, taxes and the second law of 
thermodynamics. All three are processes 
in which useful or accessible forms of some quantity, such as energy or 
money, are transformed into useless,
 inaccessible forms of the same quantity. That is not to say that these 
three processes don't have fringe benefits: 
taxes pay for roads and schools; the second law of thermodynamics drives 
cars, computers and metabolism; 
and death, at the very least, opens up tenured faculty positions.
Seth Lloyd


More information about the Chicago-talk mailing list