Operating System - HP-UX
1834425 Members
2273 Online
110067 Solutions
New Discussion

Re: How to chop off the last part of a large file?

 
SOLVED
Go to solution
John Wolfe_1
Advisor

How to chop off the last part of a large file?

Hi:

I need to chop off the last part of a very large file. The file is so large that I can't copy and move it back into place. Does anyone know of a command to allow me to do this to a file without a copy?

TIA,
John
At least I have a job.
17 REPLIES 17
Steven E. Protter
Exalted Contributor

Re: How to chop off the last part of a large file?

The tail command has always worked for me.

tail filename > newfile

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Umapathy S
Honored Contributor

Re: How to chop off the last part of a large file?

John,
There any many options.

You can use split to split the files into many chunks(files). check man split.

You can view say first 100 lines using head -100 server.out

You can also use tail to see the last part of the file say 100 lines from below tail -100 server.out.

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
Paul Sperry
Honored Contributor

Re: How to chop off the last part of a large file?

tail -n number_of_lines large_file > new_file
A. Clay Stephenson
Acclaimed Contributor

Re: How to chop off the last part of a large file?

Because it relies on the underlying truncate() system call which is not implemented in all flavor of UNIX, this is not a universal solution BUT it will work in HP-UX (and most modern UNIX's):

perl -e 'truncate("myfile",10000000)'

This will chop off everything in "myfile" beyond the 10,000,000th byte.

Man 2 truncate for details.
If it ain't broke, I can fix that.
Elena Leontieva
Esteemed Contributor

Re: How to chop off the last part of a large file?

I think it should read:

tail -N file > lastN.file
John Wolfe_1
Advisor

Re: How to chop off the last part of a large file?

Hi Steven:

I guess I was not very clear. I want to keep the first part of a file and discard the last parts without rewriting the file. I would really like to chop a file off at say line 900000 and do the operation "in place".

Thanks,
John
At least I have a job.
Jim Mallett
Honored Contributor

Re: How to chop off the last part of a large file?

Clays example will do exactly that. It looks like it's just using a temporary space and replacing the original file. No copy or redirect required.

Hindsight is 20/20
Dario_1
Trusted Contributor

Re: How to chop off the last part of a large file?

John:

You can use the head command. See the man pages for more information. It does the opposite than the tail command.

head -n 100 filename > newfilname

Regards,

Dario
A. Clay Stephenson
Acclaimed Contributor

Re: How to chop off the last part of a large file?

Actually, truncate() doesn't use any temporary space. It just goes out to a given offset and WHAM - the ax falls. The remaining data is returned to the filesystem as free space. The tricky part will be converting the start of a line number into an offset - but that ain't so hard either.

If it ain't broke, I can fix that.
Caesar_3
Esteemed Contributor

Re: How to chop off the last part of a large file?

Hello!

The perl function truncate will help you
and as A. Clay Stephenson writen that it's
not suporten on all kind of unix systems
you also have library that implement truncate
for c/c++ that you could use not shure about
the name something about filetools.

Caesar
Jim Mallett
Honored Contributor

Re: How to chop off the last part of a large file?

Thanks for the clarification Clay. I have a few points set aside here next time you respond to one of my threads.

Can't wait to finally get time to open the Perl books.
Hindsight is 20/20
Dario_1
Trusted Contributor

Re: How to chop off the last part of a large file?

Hi John!

Please disregard my approach since you will have to re-direct to a file.

Regards,

Dario
curt larson_1
Honored Contributor

Re: How to chop off the last part of a large file?

you can give this a try:

num=9000001 #number of lines to save + 1.

ex -s +"$num,$ d|wq" $yourfile

of course you might want to do some testing to verify there is at least $num lines in your file before doing this. and, I would guess that ex does create a scratch file copy of $yourfile in /var/tmp, so you'll need some free disk space there.

I'm sure there is a way to remove lines in place using perl also.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: How to chop off the last part of a large file?

Of course the perl example is perfect, but it chops the file after 10_000_000 *bytes*. This example will truncate after 9_000 *lines*:

# perl -e'$f=$ARGV[0];<>for 1..9_000;truncate$f,tell' file_to_truncate

example is for 1 file only!

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
John Wolfe_1
Advisor

Re: How to chop off the last part of a large file?

Thanks Procura. You get the 10 points! I couldn't quite figure out how to get the lines converted to bytes. A Clay hinted that it was easy but I guess he was waiting for me to figure it out myself.

Thanks to all,
John
At least I have a job.
A. Clay Stephenson
Acclaimed Contributor

Re: How to chop off the last part of a large file?

Actually I'm too dumb to figure out that trick so like a good teacher I leave that as an exercise for the students and let them think I know how to do it.

My second reason was that I question the need for such a command unless one might want to intentionally truncate a log to hide one's tracks in which case he would be well advised to make sure ctime, mtime, and atime are 'fixed' as well.
If it ain't broke, I can fix that.
Caesar_3
Esteemed Contributor

Re: How to chop off the last part of a large file?

Hello!

To figure out the size of line you will
need to know the size of line that's mean
scan all file and get size of every line
than in the end truncate what you want to
remove.

Best for you is to made the whole script on perl.

Caesar