learner
31st December 2004, 04:59
Hi,

I have a file in Unix which is some 30 mb in size , i would like to delete the first lets say 3000 lines without opening the file in vi editor ... can i do it ???

How can i delete or reduce the file size while sitting on shell prompt ?

Regards

Learner

Markus Schmitz
31st December 2004, 08:24
Hi learner,

you can do this in many ways. Here are some:

a) truncate the file

> filename

b) get the last 1000 lines of the file

tail -n 10000 filename > new_filename

b) get the first 1000 lines of the file

head -n 10000 filename > new_filename


Regards

Markus

learner
31st December 2004, 08:46
Hello,

but if some process is writing to the file, then from the shell prompt even though i give command

> logfile

then also it shows file size as same. Is it that the file size using ls -l will give me correct once this process stops writing to the file ?

Regards

Learner

Dikkie Dik
31st December 2004, 11:13
Learner,

awk '{if (NR>30000) print $0}' <your_file> > <your_new_file>

does the job you asked for. Drawback: his only works fine if awk can handle the file. E.e.g awk can't handle files with too many words on a line.

I assume you get much better feedback for these questions on UNIX scripting forums like here (http://www.tek-tips.com/threadminder.cfm?pid=822).

Hope this helps,
Dick

learner
31st December 2004, 12:12
Hi,

Thanks for the same.

Regards

Learner

Nancy Mathew
10th January 2005, 23:17
Hi,

There is a simple option to use the split command on the file.

$spilt -linecount filename prefix

the default linecount is 1000
The output will be multiple files of linecount # of rows.

Files will be named:
prefixaa
prefixab and so on

Regards,
Nancy