How can I edit a large file in place?

I have a few files sized > 1 GB each. I need to remove last few bytes from the files. How can I do it? I prefer to edit file in place to save disk space.

I am on HP-UX.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Cut 2 kilobytes from end of file:

truncate -s-2K file

Method 2

Try using hexedit I haven’t tried it on HP-UX but it should work. It allows you to move to a location in a file and truncate. I’m pretty sure that it does not read the whole file in but just seeks to the appropriate location for display.

Usage is fairly simple once you have launched it the arrow keys allow you to move around. F1 gives help. Ctrl-G moves to a location in the file (hint: to move to end use the size of the file from the bottom row of the display). Position the cursor on the first byte that you want to truncate and then press Escape T once you confirm the truncate will have been done. Ctrl-x exits.

Method 3

Use a tool that gives you access to the truncate system call. You can do it with only POSIX tools. Warning, typed into a browser; be especially careful as dd is even more unforgiving of errors than the usual unix command. 123456 is the number of bytes to keep.

dd if=/dev/null of=/file/to/truncate seek=1 bs=123456

A Perl version is much more readable:

perl -e 'truncate "$ARGV[0]", 123456 or die $!' /file/to/truncate

Method 4

You can use dd for example:

dd if=yourfile of=outname bs=4k count=thefirstX4kb

Method 5

You can use split or ed, awk or any programming language.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x