Chinaunix首页 | 论坛 | 博客
  • 博客访问: 517918
  • 博文数量: 47
  • 博客积分: 1045
  • 博客等级: 准尉
  • 技术积分: 663
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-09 09:27
文章存档

2012年(47)

分类: LINUX

2012-09-20 16:37:12


一个大型文件(总之不小),要求删除该文件的最后一行,求一种效率比较高的解决方法。 测试用的文本文件800M
1.用sed解决,此法最易想,但也是最笨的一个,
解决方法来自问题的提出者:
sed -e '$d' input.file > output.file
用time测试了一下,效率是相当的低!
real    2m51.099s
user    2m1.268s
sys    0m4.260s
2.用head解决,此法比sed有一个质的的提升,提升来自增大了缓存,不过依然没有抓住问题的本质,还是做了不少无用功!解决方法来时cu上的热心网友。
head -n-1 input.file > output.file
real    0m23.687s
user    0m0.212s
sys    0m4.668s
3.用vim解决,此法很别处心裁,这应该是遇到这个问题的最先想到的一种。解决方法来自我加的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写效率最高,但显然,代码也是最长的,我实现了代码,
测试了一下,
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;
}

阅读(2123) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~