我想数数ASCII文本文件中的行数。我认为最好的方法是数数文件中的换行符:
for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) { /* Count word line endings. */
if (c == '\n') ++lines;
}
但是,我不确定这是否能解释所有和Linux的最后一行。也就是说,如果我的文本文件在没有显式换行符的情况下以下面的方式结束,那么是否有一个编码在其中,还是应该在for循环之后添加一个额外的++lines;?
cat
dog
那么如果文件末尾有一个显式换行符呢?还是只需要通过跟踪以前读
我需要检查日志文件中是否有一个文本,如server crashed,即,而不是,后面是其他文本server is up again (因为如果我发现这种情况,就意味着我必须手动启动服务器)。当然,这些文本之间可能有许多不同的行文。我想使用任何简单的linux命令行工具,例如。但是我没能创建一个regex
如何用]替换文件的最终字符?
例如,如果文档内容如下所示:
This is the first line with a full stop.
This is the second line with a full stop.
This is the third line with square bracket.
我希望它以后看起来像这样:
This is the first line with a full stop.
This is the second line with a full stop.
This is the third line with square bracket]