PIKT

Samples: Truncate File

PIKT Logo
Home FAQ News Intro Samples Tutorial Reference Software Authors Licensing SiteSearch

Computer Magazines at Amazon.com

Linux Journal
Linux Journal

Computer Security Journal
Computer Security Journal

Computers & Security
Computers & Security

Network Computing
Network Computing

Journal Of Network And Computer Applications
Journal Of Network And Computer Applications

trunc_programs.cfg defines a simple Perl script to truncate files, for example logs and other data files.  Many Pikt scripts invoke trunc.pl.

///////////////////////////////////////////////////////////////////////////////
//
// trunc_programs.cfg
//
///////////////////////////////////////////////////////////////////////////////

trunc.pl        // truncate a file

        #!=perl
        
        # trunc.pl -- truncate a file
        
        die "Usage:  trunc.pl <#lines> <file>\n" unless $#ARGV == 1 ;
        
        $LINES = $ARGV[0] ;
        $FILE = $ARGV[1] ;
        
        $BAK = "$FILE.bak.gz" ;
        $TMP = "$FILE.tmp" ;
        
        die "no $FILE file!\n" if (! -e $FILE) ;
        
        ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,
         $mtime,$ctime,$blksize,$blocks) = stat($FILE) ;
        
        unlink($BAK) ;
        system("=gzip -c $FILE > $BAK") ;
        chown($uid, $gid, $BAK) ;
        chmod($mode, $BAK) ;
        
        system("=tail -$LINES $FILE > $TMP") ;
        chown($uid, $gid, $TMP) ;
        chmod($mode, $TMP) ;
        rename($TMP, $FILE) ;

//////////////////////////////////////////////////////////////////////////////
On the piktmaster system, we would install the trunc.pl script to each PIKT slave system with the command:
# piktc -iv +P trunc.pl +H all        [or: ... -H downsys]

[For more examples, see Samples.]

Home | FAQ | News | Intro | Samples | Tutorial | Reference | Software | Authors | Licensing | SiteSearch
Links | SiteIndex | Pikt-Users | Pikt-Workers | Contribute | ContactUs | Top of Page
Page best viewed at 1024x768.   Page last updated 2005-06-22.
This site is PIKT® powered.
PIKT® is a registered trademark of the University of Chicago.
Copyright © 1998-2005 Robert Osterlund.  All rights reserved.

Computer Books at Amazon.com

Randal Schwartz's Perls of Wisdom
Randal Schwartz's Perls of Wisdom

Perl CD Bookshelf
Perl CD Bookshelf

Perl and CGI for the World Wide Web
Perl and CGI for the World Wide Web

Higher-Order Perl: Transforming Programs with Programs
Higher-Order Perl: Transforming Programs with Programs

CGI Programming with Perl
CGI Programming with Perl