Remove the contents of a file without deleting the file

Hi All,

I want to delete the contents of few files which are really huge in size.
How the same can be done by using sed. Is there any other alternative to sed.

Thanks in advance

Without using sed.

> yourfile.txt

It worked!! Thanks Vino

sed '1q' inputfile >tmp
mv tmp inputfile

or

sed '' ipfile > ipfile

for huge files,
this would be faster

cp /dev/null file

sed '1q' inputfile >tmp
mv tmp inputfile

will not remove the first line.

sed '' filename > filename works!!

Madhan,
Can u pls explain cp /dev/null.. What is the advantage it brings over sed/ others...

Thanks
Sumesh

FWIW -

> file

is the fastest call, as it truncates the file. And any command, like the sed example above, that has "sed ' ' file > file" in it already sets the file size to zero before running sed. So the sed call (or any call ) is doing nothing because the file is already empty before sed executes.

Forgot to add -n option

sed -n '1q' inputfile >tmp
mv tmp inputfile 

Thanks to all for providing the info.

I'm sure you got a good solution by now, but, just wanted to add that this also should work...

cat /dev/null > filename

#!/usr/bin/perl
$file=join(" ",@ARGV);
if($file eq ""){
die "Usage:\n./$0 FILENAME";
}
open(FD,"> $file")|| die "Can't open to WRONLY";
print FD "";
close(FD);

----------------------cut here--------------------
perl :smiley: