Is this the most efficient way to get and remove first line in file?

前端 未结 12 2257
天命终不由人
天命终不由人 2020-12-03 03:53

I have a script which, each time is called, gets the first line of a file. Each line is known to be exactly of the same length (32 alphanumeric chars) and terminates with \"

12条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 04:08

    I wouldn't usually recommend opening up a shell for this sort of thing, but if you're doing this infrequently on really large files, there's probably something to be said for:

    $lines = `wc -l myfile` - 1;
    `tail -n $lines myfile > newfile`;
    

    It's simple, and it doesn't involve reading the whole file into memory.

    I wouldn't recommend this for small files, or extremely frequent use though. The overhead's too high.

提交回复
热议问题