unix 下统计文件的个数

shenghuiping2001 2010-05-26 01:18:56
还是这个问题:
我在一个文件夹下:有许多的LOG文件:
log20100525-192536
log20100525-202552
log20100526-092516
.....

格式是logYYYYmmdd-hhmmss

想统计一下每天的LOG 数是多少

就不知道怎么实现啊?
...全文
436 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
louyong0571 2010-06-02
  • 打赏
  • 举报
回复
主席的应该可行!建议试下
Tony2251 2010-06-02
  • 打赏
  • 举报
回复
用简单的脚本应该可以实现的,学些~!
FgLee 2010-05-26
  • 打赏
  • 举报
回复
不好意思,修改了以下,好像可以了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
char tmpbuf[64];
char buf[64];
char date[12];
int num = 0;
FILE *fp = NULL;


system("ls ./mydir > tmp.txt");
if (access("./tmp.txt",F_OK) == 0)
{
if((fp = fopen("./tmp.txt","r")) == NULL)
{
perror("open error");
return -1;
}

if(fgets(buf,sizeof(buf),fp))
{
num++;
while(fgets(tmpbuf,sizeof(tmpbuf),fp))
{
if (strncmp(buf,tmpbuf,11) == 0)
num++;
else
{
snprintf(date,12,buf);
strcpy(buf,tmpbuf);
printf("%s: %d\n",date,num);
num = 1;
}
}
snprintf(date,12,buf);
printf("%s: %d\n",date,num);
}
}

return 0;
}


FgLee 2010-05-26
  • 打赏
  • 举报
回复
// 其中: mydir 是你要统计的日志目录
// 编译后,放在和mydir 同级目录下运行

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
char line[32];
char tmpline[32];
char buf[64];
int num = 0;
FILE *fp = NULL;


system("ls ./mydir > tmp.txt");
if (access("./tmp.txt",F_OK) == 0)
{
if((fp = fopen("./tmp.txt","r")) == NULL)
{
perror("open error");
return -1;
}

if(fgets(buf,sizeof(buf),fp))
{
num++;
strncpy(line,buf,11);
while(fgets(buf,sizeof(buf),fp))
{
strncpy(tmpline,buf,11);
if (strcmp(line,tmpline) == 0)
num++;
else
{
strcpy(line,tmpline);
printf("%s: %d\n",line+3,num);
num = 1;
}
}
}
}

return 0;
}
yifanernei 2010-05-26
  • 打赏
  • 举报
回复
怀疑版本问题就把你的环境说出来嘛,这样也方便大家帮助你不是
steptodream 2010-05-26
  • 打赏
  • 举报
回复
ls log* |awk '{print substr($NF,4,8)}'|sort |uniq -c
lvyuancyx 2010-05-26
  • 打赏
  • 举报
回复
ls log*| sed 's/log\(.*\)-.*/\1/' | sort | uniq -c
ls *log* |awk '{print substr($NF,4,8)}'|sort -n|uniq -dc
handledos 2010-05-26
  • 打赏
  • 举报
回复
ls |grep log |wc -l

shenghuiping2001 2010-05-26
  • 打赏
  • 举报
回复
lz 的兄弟,我试了一下:报错:

sed -e ecpression #1.char17 :unrerminated 's' command

可能是我的LINUX 的版本的问提吧,不好意思,还有没有其它的COMMAND?
yifanernei 2010-05-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lylm 的回复:]
sed 's/log\(.*\)-.*/\1/' //替换文件名为,log后,-之前的部分(也就是日期)
如log20100525-192536替换为20100525
这样再sort|uniq后就能排序按天统计
[/Quote]
没有错,如果你的目录里有别的文件,可以把ls换成
ls log????????-*
或者在sed中先把不合规则的文件名排除一下
lylm 2010-05-26
  • 打赏
  • 举报
回复
sed 's/log\(.*\)-.*/\1/' //替换文件名为,log后,-之前的部分(也就是日期)
如log20100525-192536替换为20100525
这样再sort|uniq后就能排序按天统计
shenghuiping2001 2010-05-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yifanernei 的回复:]
C/C++ code
ls | sed 's/log\(.*\)-.*/\1/' | sort | uniq -c
[/Quote]

兄弟,能说一下:'s/log\(.*\)-.*/\1/' 是啥意思不?
yifanernei 2010-05-26
  • 打赏
  • 举报
回复
ls | sed 's/log\(.*\)-.*/\1/' | sort | uniq -c

18,772

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧