From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Rich Glazier Newsgroups: gmane.os.tru64.managers Subject: SUMMARY: cropping a file Date: Fri, 13 Feb 2004 11:43:27 -0800 (PST) Sender: tru64-unix-managers-owner@ornl.gov Message-ID: <20040213194327.8751.qmail@web20725.mail.yahoo.com> References: <20040211184624.43655.qmail@web20727.mail.yahoo.com> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1076701494 20544 80.91.224.253 (13 Feb 2004 19:44:54 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 13 Feb 2004 19:44:54 +0000 (UTC) Original-X-From: tru64-unix-managers-owner-gotm-tru64-unix-managers=m.gmane.org@sws1.ctd.ornl.gov Fri Feb 13 20:44:47 2004 Return-path: Original-Received: from sws1.ornl.gov ([160.91.1.101]) by deer.gmane.org with smtp (Exim 3.35 #1 (Debian)) id 1ArjF0-0003kD-00 for ; Fri, 13 Feb 2004 20:44:47 +0100 Original-Received: (qmail 18587 invoked by uid 5); 13 Feb 2004 19:44:40 -0000 Original-Followup-To: poster Precedence: bulk Original-Received: (qmail 18572 invoked by uid 49493); 13 Feb 2004 19:44:40 -0000 Original-Received: from rglazier2002@yahoo.com by sws1 by uid 49491 with qmail-scanner-1.16 (iscan: v3.1/v5.600-1011/622/49252. clamscan: 20030605. spamassassin: 2.55. Clear:SA:0(0/0):. Processed in 64.252321 secs); 13 Feb 2004 19:44:40 -0000 Original-Received: from unknown (HELO emroute1.cind.ornl.gov) (160.91.4.119) by 0 with SMTP; 13 Feb 2004 19:43:36 -0000 Original-Received: from emroute1.cind.ornl.gov (localhost [127.0.0.1]) by emroute1.cind.ornl.gov (PMDF V6.2-X17 #30669) with ESMTP id <0HT100JA6G4H2E@emroute1.cind.ornl.gov> for tru64-unix-managers@sws1.ctd.ornl.gov (ORCPT tru64-unix-managers@ornl.gov) ; Fri, 13 Feb 2004 14:43:35 -0500 (EST) Original-Received: from web20725.mail.yahoo.com (web20725.mail.yahoo.com [216.136.226.107]) by emroute1.cind.ornl.gov (PMDF V6.2-X17 #30669) with SMTP id <0HT100F6PG4FAE@emroute1.cind.ornl.gov> for tru64-unix-managers@sws1.ctd.ornl.gov (ORCPT tru64-unix-managers@ornl.gov) ; Fri, 13 Feb 2004 14:43:28 -0500 (EST) Original-Received: from [65.201.47.246] by web20725.mail.yahoo.com via HTTP; Fri, 13 Feb 2004 11:43:27 -0800 (PST) In-reply-to: <20040211184624.43655.qmail@web20727.mail.yahoo.com> Original-To: tru64-unix-managers@ornl.gov Xref: main.gmane.org gmane.os.tru64.managers:6440 X-Report-Spam: http://spam.gmane.org/gmane.os.tru64.managers:6440 I thought there was a way using /dev/null, but I think I was mistaken. Here are some of the suggestions. Thanks to everyone. (Chris Ruhnke, Bob Marcan, Alan Rollow, Steve Feehan, Elin Vaeth, Jim Fitzmaurice, Paul A. Sand, James Sainbury, Phillip Brown, John Farmer) I ended up creating a cropped temp file, and then copying it over the original: tail -49 /usr/scripts/prod_on_mars.out > /tmp/newfile rm /usr/scripts/prod_on_mars.out mv /tmp/newfile /usr/scripts/prod_on_mars.out ---------- Hi, Here is a solution. Let me know if you find a more elegant way to do this: Put the following into a file: ed filename.ext << _HERE_ 1,100d w _HERE_ The above translates as use the 'ed' editor to edit filename.ext. Accept input to 'ed' until the string _HERE_ is encountered. 1,100d Starting at line 1 delete 100 lines w Write buffer contents _HERE_ exit Although this will execute very quickly (assuming logfile isn't very huge) there is a small window of opportunity for the application that owns the file to experience a synchronization problem. Consider: The logfile is being modified by two sources. The owner of The file and your program. When the 'ed' command is executed file contents are read into a buffer but the file is not locked. There is potential for the source file contents to change while ed is deleting the first 100 lines. At the end of 100 line deletion ed writes his copy of the contents back to the logfile which will not include the updates that took place between the time ed read the file and the time he executes the 'w' command. Let me know if this works for you. John Farmer ----------------------- Try this: # sed -n 1,100p filename Regards, Phillip S. Brown ----------------------- I'm not sure what you mean by "dump", but I think this truncates the file at 100 lines: perl -pi -e 'exit if ($. > 100)' file I think this creates a temporary file, but the details are at least hidden from you. Paul A. Sand ---------------------- Just off the top of my head, I'd use wc -l to get a line count, subract 100 from that number then use tail - and replace the file with that result. Something like: LINECOUNT=`cat $LOGFILE | wc -l` NEWCOUNT=`expr $LINECOUNT - 100` tail -$NEWCOUNT $LOGFILE > $NEWFILE rm $LOGFILE mv $NEWFILE $LOGFILE exit 0 Jim Fitzmaurice ----------------------- Perhaps the 'split' command will give you what you want. Regards, Elin Vaeth --- Rich Glazier wrote: > Does anyone have a suggestion about how to crop a > file > such that it always remain a certain size (in one > step- i.e. not creating and deling files and not an > editor)? I have a log file where I want to dump the > fist 100 lines once a week. I've tried ways of > using > head and redirecting dev/null to the file, but can't > quite get what I want. > > Thanks. > > T645.1A PK6 > > __________________________________ > Do you Yahoo!? > Yahoo! Finance: Get your refund fast by filing > online. > http://taxes.yahoo.com/filing.html __________________________________ Do you Yahoo!? Yahoo! Finance: Get your refund fast by filing online. http://taxes.yahoo.com/filing.html