- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- script to trim the file size base on size
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-02-2004 02:35 PM
02-02-2004 02:35 PM
As above, i don't know how to write the script to trim the file size base on size. Means, let say i want to trim the file from 200mb to 50mb. Any guidance?
Rgs
Cheng Wee
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 02:53 PM
02-02-2004 02:53 PM
Re: script to trim the file size base on size
Actually the file itself to trim down size must be edited to lessen the size of it. you can use vi then delete some unnecessary lines .
vi filename
then type dd to lines not so important.
then save the file by typing wq! then enter.
Ferdie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 03:13 PM
02-02-2004 03:13 PM
Re: script to trim the file size base on size
You can do it using SAM.
If you are planning to trim the system related log files then the best method is to use sam.
From SAM --> Routine Tasks --> System Log Files
From here either you can trim the file to recommended or manually specified size.
Also you can add custom files to the list. Add whichever file that you want to reduce the size to the list from the Actions menu. Then follow the same procedure for trimming it.
Regards,
Karthik S S
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 04:55 PM
02-02-2004 04:55 PM
Re: script to trim the file size base on size
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 05:11 PM
02-02-2004 05:11 PM
Re: script to trim the file size base on size
cp syslog.log OLDsyslog.log
tail -20 OLDsyslog.log > syslog.log
At least, that's what might do what you want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 05:16 PM
02-02-2004 05:16 PM
Re: script to trim the file size base on size
We can roughly relate the size of the file with respect to the number of lines. Let us assume that the file size is 10MB and the number of lines are 50000. If you want to trim the size to 5MB then we know that we need approximately delete 25000 lines from the top. Then using tail redirect only the last 25000 lines to a new file and then move it back to overwrite the large original file. We can also use sed for this.
-Karthik S S
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 05:16 PM
02-02-2004 05:16 PM
Re: script to trim the file size base on size
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 05:23 PM
02-02-2004 05:23 PM
Re: script to trim the file size base on size
sks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 05:28 PM
02-02-2004 05:28 PM
Re: script to trim the file size base on size
$tail -c 15 a.sh
While trying same with head, I am finding some error.
sks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 05:32 PM
02-02-2004 05:32 PM
Re: script to trim the file size base on size
$head -c -n2 a.sh
where -c: The quantity of output is measured in bytes.
sks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 05:32 PM
02-02-2004 05:32 PM
Re: script to trim the file size base on size
head -c -n (byte count) filename
-Karthik S S
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 06:02 PM
02-02-2004 06:02 PM
Re: script to trim the file size base on size
run it with "logtrunk.pl
#!/usr/bin/perl
($FILENAME,$NEWSIZE)=@ARGV;
$SIZE=(stat $FILENAME)[7];
$LOSEAMOUNT=$SIZE-$NEWSIZE;
open FILE, "<$FILENAME";
open TRIMEDFILE, ">$FILENAME"."trim";
read FILE, $WASTE, $LOSEAMOUNT;
while(
print TRIMEDFILE;
}
close FILE;
close TRIMEDFILE;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 06:04 PM
02-02-2004 06:04 PM
Re: script to trim the file size base on size
$split -b 200 file_name
Check man pages for more options.
sks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2004 08:00 PM
02-02-2004 08:00 PM
Re: script to trim the file size base on size
You can't use "tail" for large files, because it only works in a 20K buffer.
You also need to be careful if applications are holding files open - you don't want to rename the original file, copy the last 50MB back to the orignal file name, then delete the oriignal file, because the applications will continue writing to the original (now "deleted") file, and continue to eat up disk space.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 01:23 AM
02-03-2004 01:23 AM
Solution#!/usr/bin/perl
if ($#ARGV >= 1)
{
$fname = "+< $ARGV[0]";
open FH1, $fname or die "Can't open $ARGV[0]\n";
truncate(FH1,$ARGV[1]);
close FH1;
}
For example,
truncate.pl myfile 5000
would open myfile and then truncate it to exactly 5000 bytes.
This is very useful in cases where you are short of space and cannot copy the contents to another file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 01:35 AM
02-03-2004 01:35 AM
Re: script to trim the file size base on size
You could use "dd" with the count= parameter.
dd bs=1024 count=50k if=oldfile of=newfile
This will created newfile at precisely 50mb. Then you could "mv newfile oldfile" to replace the original file.
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 11:45 AM
02-03-2004 11:45 AM
Re: script to trim the file size base on size
Sorry for the late reply, been very busy yesterday. Just to clarify one thing, is really work on ascii file, i asking about size is because i wonder how sam can trim log file until recommended size. I interesting with the perl script that can truncate without need to copy the file to another place first and also the concern on not delete the original file. I will look at this in more detail.
Appreciate all replies.
Rgs
Cheng Wee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 05:01 PM
02-03-2004 05:01 PM
Re: script to trim the file size base on size
>-------------------------------------------------------------------------------
>Cheng
>You can't use "tail" for large files, >because it only works in a 20K buffer.
Nah... that would be soooo seventies!
You can wag that tail well over 20K...
Don't know what the limit (if any) is.
$ uname -a
HP-UX B.11.23 U ia64
$ tail -1000 kc.log > x
$ ls -l x
39059 Feb 4 00:53 x
$ tail -c -10000 kc.log > x
$ ls -l x
10000 Feb 4 00:49 x
$ tail -c -50000 kc.log > x
$ ls -l x
50000 Feb 4 00:49 x
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 05:20 PM
02-03-2004 05:20 PM
Re: script to trim the file size base on size
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2004 09:11 PM
02-03-2004 09:11 PM
Re: script to trim the file size base on size
11.11 seems to be limited to 20K:
cd /var/adm/sw
wc -l swagent.log
8955 swagent.log
tail -4000 swagent.log > /tmp/at
ll /tmp/at
(size = 20480)
wc -l /tmp/at
564 /tmp/at
tail -c -45000 swagent.log > /tmp/atc
ll /tmp/atc
(size = 20480)
also see WARNINGS in 'man tail'
By the way - regarding Clay's response (note his use of "exactly") - this will truncate the file, i.e. chop off the end, and may not be what you want if you would prefer to retain the newest, rather than oldest, records.
One possible approach would be:
a)calculate the file size (ls -s gives it in blocks)
b) work out how many blocks to skip
c) use dd skip=
d) cat logtemp > logfile
e) rm logtemp
The trouble with this approach is that records added after step c) may be lost, or mingle in with the records being copied in from logtemp.
I had a slightly related problem to solve a few months ago, but in my case, the log file was the captured output from a script which spawned a couple of hundred sub-processes. What I did was to replace ">>log" with "|script.pl" where script.pl managed the log file (log and log.OLD) after a certain number of lines had been written.