一個大型文件(總之不小),要求刪除該文件的最后一行,求一種效率比較高的解決方法。
測試用的文本文件800M
1.用sed解決,此法最易想,但也是最笨的一個,
解決方法來自問題的提出者:
sed -e '$d' input.file > output.file
用time測試了一下,效率是相當?shù)牡停?br />
real 2m51.099s
user 2m1.268s
sys 0m4.260s
2.用head解決,此法比sed有一個質(zhì)的的提升,提升來自增大了緩存,不過依然沒有抓住問題的本質(zhì),還是做了不少無用功!解決方法來時cu上的熱心網(wǎng)友。
head -n-1 input.file > output.file
real 0m23.687s
user 0m0.212s
sys 0m4.668s
3.用vim解決,此法很別處心裁,這應(yīng)該是遇到這個問題的最先想到的一種。解決方法來自我加的unix like群里的一個叫石仔的管理員!
vim + result
dd
這個沒測試,感覺效率和head法差不多,加載太慢!
4.重量級要到場了,感謝cu版主的這個腳本,只能用四個字形容!五體投地!
:|dd of=input.file seek=1 bs=$(($(find input.file -printf "%s")-$(tail -1 input.file|wc -c)))
或者是
:|dd of=input.file seek=1 bs=$(($(stat -c%s input.file)-$(tail -1 input.file|wc -c)))
測試了一下!
real 0m0.123s
user 0m0.004s
sys 0m0.012s
5.感覺這個用c寫效率最高,但顯然,代碼也是最長的,我實現(xiàn)了代碼,
測試了一下,
real 0m0.002s
user 0m0.000s
sys 0m0.000s
代碼如下:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#define GUESS_LINE_SIZE 80
int get_line_size(char *ptr);
int
main(int argc, char *argv[])
{
char buf[GUESS_LINE_SIZE];
int line_len, fd;
struct stat stat_buf;
fd = open(argv[1], O_RDWR);
lstat(argv[1], &stat_buf);
lseek(fd, -GUESS_LINE_SIZE, SEEK_END);
read(fd, buf, GUESS_LINE_SIZE) ;
line_len = get_line_size(buf);
truncate(argv[1], stat_buf.st_size - line_len);
exit(0);
}
int
get_line_size(char *ptr)
{
int line_len = 0, i = GUESS_LINE_SIZE - 2;/*buf中的最后一個字符為'\n'*/
while (*(ptr + i) != '\n') {
//printf("%c", *(ptr + i));
i--;
line_len++;
}
return line_len;
}
|
posted on 2010-04-21 18:45
xzc 閱讀(3289)
評論(2) 編輯 收藏 所屬分類:
linux/unix