- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Direct trimming of a logfile
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:19 AM
02-06-2004 03:19 AM
I have an unusual request. I have a very large logfile that I would like to trim each day. I would like to remove perhaps the first 5000 lines and keep the rest of the file. I do not want to create a new file but rather edit this logfile directly using a script. Any ideas?
Thanks,
David
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:33 AM
02-06-2004 03:33 AM
Re: Direct trimming of a logfile
tail 10000 logfilename > newlogname
cp logfilename /tmp/logfilearchive
mv newlogname logfilename
Let cron do it once a day.
This insures you get the last 10000 lines of the log file in question every day. It is an effective trim.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:38 AM
02-06-2004 03:38 AM
Re: Direct trimming of a logfile
You could use cat $LOG head -n 5000 >> $LOG2 && mv LOG2 LOG ,
But this will create a new file.
Gideon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:39 AM
02-06-2004 03:39 AM
SolutionThe trick is to open two filehandles on the same pathname: one for input and the other for update. We read off the first lines and then when the first line to keep is found we start copying to the update filehandle. When done, we use the tell() function to determine the offset for the last write operation and truncate the file at that point. It's probably taken me longer to 'splain this than it did to write the Perl code using a bit of copy and paste.
Here's trunc.pl; Use it like trunc.pl mylogfile 5000 to throw away the 1st 5000 lines of mylogfile.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:44 AM
02-06-2004 03:44 AM
Re: Direct trimming of a logfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:45 AM
02-06-2004 03:45 AM
Re: Direct trimming of a logfile
I don't like to use tail for more than 100-200 lines due to this limitation.
My 2 cents,
Jeff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:50 AM
02-06-2004 03:50 AM
Re: Direct trimming of a logfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:55 AM
02-06-2004 03:55 AM
Re: Direct trimming of a logfile
Yes, the logfile is a large ASCII text file.
Thanks for all the ideas so far. I especially like A. Clay's approach. Clay, would it be possible to capture the first lines in another file instead of just ingoring them them?
Thanks,
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:56 AM
02-06-2004 03:56 AM
Re: Direct trimming of a logfile
i.e a file of size 200K splitted in 15ks would have 13 of 15K and 1 of 5K. The last 1K would be the last but if we split from the end the 5K would be the 1st.
Let me check.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:57 AM
02-06-2004 03:57 AM
Re: Direct trimming of a logfile
"Logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large."
http://hpux.ee.ualberta.ca/hppd/hpux/Sysadmin/logrotate-2.5/
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:58 AM
02-06-2004 03:58 AM
Re: Direct trimming of a logfile
I'll give it another hack then.
#!/usr/contrib/bin/perl
$Argument = ($ARGV[0]);
$instance = $Argument;
$countfile = "/var/adm/syslog.log";
$newlog = "/var/adm/syslog.log.new";
open (COUNT, "$countfile");
open (NEWLOG, "$newlog");
# print "Made it here bud!!!!\n";
$n1 = 0;
@filedata =
foreach $filedata (@filedata){
chop ($filedata) if ($filedata =~/\n$/);
# print "$filedata eol \n";
$filetoback = $filedata;
$n1 = $n1 + 1;
print NEWLOG "$filetoback";
if ($count ge 50000) {
close(COUNT);
close(NEWLOG);
exit;
}
}
The chop command takes off carraige return line feed. You probably don't want to do that.
You will have to adjustments to get the LAST 50000 lines of the log.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 03:59 AM
02-06-2004 03:59 AM
Re: Direct trimming of a logfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 04:01 AM
02-06-2004 04:01 AM
Re: Direct trimming of a logfile
"print $s;" line just before the "++$knt;" line in the first while loop. That will send the lines to be discarded to stdout. In the case where those lines truly don't need to be kept then simply redirect stdout to /dev/null.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 04:07 AM
02-06-2004 04:07 AM
Re: Direct trimming of a logfile
LINE= `wc -l logfile
get the value minus 500 lines= NEWLINE
then split using the -l of the value of NEWLINE.
this will create 2 files one would be very big and the other will be what you need.
delete what you do not need and rename the fiel of size 500 lines.
You may need to add some logics as you go along. This is simple for any level
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2004 04:10 AM
02-06-2004 04:10 AM
Re: Direct trimming of a logfile
Thanks to all.
David