views:

835

answers:

9

Hey all,

I have a very large text file and all I need to do is remove one single line from the top of the file. Ideally, it would be done in PHP, but any unix command would work fine. I'm thinking I can just stream through the beginning of the file till I reach \n, but I'm not sure how I do that.

Thanks, Matt Mueller

A: 

If your file is flat, you can use sed '1d' file > newfile

Soufiane Hassou
A: 

I don't know how big is your file, but did you try awk 'NR > 1' {print} ?

abyx
awk is too big cannon for this job. If he wants to do this operation regulary (scripted), awk is not 'great' solution
Martin Kopta
@Martin, explain too big cannon. awk is one of the fastest file processing tool available in *nix. If you don't know anything, don't comment.
What's the difference between awk and sed in this respect?
abyx
A: 

I'm a bit rusty on perl, but this might do the trick:

#!/usr/bin/perl
$first = true;
while (<>)
{
    if ($first)
    {
        # skip first line
        $first = false;
    }
    else
    {
        print;
    }
}

and use this script as a filter:

cat myfile.txt | removefirstline.pl > myfile_2.txt
Daren Thomas
This is probably overkill considering it can be done with basic unix text manipulation tools ;).
Conrad Meyer
its not overkill. Perl is just another tool to do the job. >>perl -ne 'print if $. > 1' file
wow. perl one-liners allways freak me out. Love it!
Daren Thomas
A: 

sed -n '2,$p' file

Martin Kopta
and -i for replace original file content
Martin Kopta
+1  A: 

Assuming tail from GNU coreutils:

tail -n +2 file > newfile
ysth
+2  A: 

sed -i -e '1d' file will do what you want.

  • -i indicates "in-place"
  • -e means "evaluate this expression"
  • '1d' means, delete the first line
Conrad Meyer
"delete" is expensive. why not just print from line 2 onwards.
+1  A: 
tail -n +2 < source > destination

Tail with positive number outputs everything starting with N-th line.

yu_sha
depends on system and version of tail
Martin Kopta
+5  A: 

you can use a variety of tools in *nix. A comparison of some of the different methods on a file with more than 1.5 million lines.

$ wc -l < file4
1700589

$ time sed -n '2,$p' file4 > /dev/null

real    0m2.538s
user    0m1.787s
sys     0m0.282s

$ time awk 'NR>1' file4 > /dev/null

real    0m2.174s
user    0m1.706s
sys     0m0.293s

$ time tail -n +2 file4 >/dev/null

real    0m0.264s
user    0m0.067s
sys     0m0.194s

$time  more +2 file4 > /dev/null

real    0m11.771s
user    0m11.131s
sys     0m0.225s

$ time perl -ne 'print if $. > 1' file4 >/dev/null

real    0m3.592s
user    0m3.259s
sys     0m0.321s
+1 for the timings of the various solutions, all of which work and are a lot more elegant than some of other suggestions here
I82Much
A: 
function cutline($filename,$line_no=-1) {

$strip_return=FALSE;

$data=file($filename);
$pipe=fopen($filename,'w');
$size=count($data);

if($line_no==-1) $skip=$size-1;
else $skip=$line_no-1;

for($line=0;$line<$size;$line++)
if($line!=$skip)
fputs($pipe,$data[$line]);
else
$strip_return=TRUE;

return $strip_return;
}

cutline('foo.txt',1); // deletes line 1 in foo.txt
}
streetparade