带有PHP尾部的奇数-n 1返回多个结果 [英] Oddity with PHP tail -n 1 returning multiple results

查看:48
本文介绍了带有PHP尾部的奇数-n 1返回多个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此问题... 回答并很好,也是.但是,出现了奇怪的情况,即如果日志文件具有唯一的最后一行(即,前几个单词与前面的行不同),它将正确地以 tail -n 1 "file" 返回最后一行,但是如果最后一行几行类似于最后一行,它返回所有相似的行.

I had this question... answered and very nice it was too. But, oddity has emerged whereby if the log file has a unique last line, (i.e. the first few words are different to the preceeding lines) it correctly returns that last line with tail -n 1 "file" but if the last few lines are similar to the the last line, it returns all the lines that are similar.

让我告诉你....

正在读取的文件是...

The file it's reading is...

frame= 1065 fps= 30 q=1.6 size=   11977kB time=35.54 bitrate=2761.1kbits/s    
frame= 1081 fps= 30 q=2.7 size=   12174kB time=36.07 bitrate=2765.0kbits/s    
frame= 1097 fps= 30 q=2.7 size=   12332kB time=36.60 bitrate=2759.9kbits/s    
frame= 1113 fps= 30 q=3.0 size=   12487kB time=37.14 bitrate=2754.4kbits/s    
frame= 1129 fps= 30 q=2.4 size=   12652kB time=37.67 bitrate=2751.3kbits/s    
frame= 1145 fps= 30 q=2.4 size=   12824kB time=38.20 bitrate=2749.7kbits/s    
frame= 1161 fps= 30 q=2.4 size=   12996kB time=38.74 bitrate=2748.1kbits/s    
frame= 1176 fps= 30 q=2.7 size=   13162kB time=39.24 bitrate=2747.8kbits/s    
frame= 1191 fps= 30 q=2.6 size=   13328kB time=39.74 bitrate=2747.4kbits/s    
frame= 1206 fps= 30 q=2.5 size=   13496kB time=40.24 bitrate=2747.5kbits/s    
frame= 1222 fps= 30 q=2.5 size=   13685kB time=40.77 bitrate=2749.6kbits/s    
frame= 1240 fps= 30 q=4.2 size=   13954kB time=41.38 bitrate=2762.8kbits/s    
frame= 1261 fps= 31 q=4.6 Lsize=   14428kB time=42.08 bitrate=2809.1kbits/s    
video:13889kB audio:494kB global headers:0kB muxing overhead 0.314239%

$line = `tail -n 1 "$logfile"`;

退货...

video:13889kB audio:494kB global headers:0kB muxing overhead 0.314239%

但是,如果最后一行,更独特的行不存在...它将返回:-

However, if that last, more unique line, isn't there... it returns:-

frame= 1065 fps= 30 q=1.6 size=   11977kB time=35.54 bitrate=2761.1kbits/s    
frame= 1081 fps= 30 q=2.7 size=   12174kB time=36.07 bitrate=2765.0kbits/s    
frame= 1097 fps= 30 q=2.7 size=   12332kB time=36.60 bitrate=2759.9kbits/s    
frame= 1113 fps= 30 q=3.0 size=   12487kB time=37.14 bitrate=2754.4kbits/s    
frame= 1129 fps= 30 q=2.4 size=   12652kB time=37.67 bitrate=2751.3kbits/s    
frame= 1145 fps= 30 q=2.4 size=   12824kB time=38.20 bitrate=2749.7kbits/s    
frame= 1161 fps= 30 q=2.4 size=   12996kB time=38.74 bitrate=2748.1kbits/s    
frame= 1176 fps= 30 q=2.7 size=   13162kB time=39.24 bitrate=2747.8kbits/s    
frame= 1191 fps= 30 q=2.6 size=   13328kB time=39.74 bitrate=2747.4kbits/s    
frame= 1206 fps= 30 q=2.5 size=   13496kB time=40.24 bitrate=2747.5kbits/s    
frame= 1222 fps= 30 q=2.5 size=   13685kB time=40.77 bitrate=2749.6kbits/s    
frame= 1240 fps= 30 q=4.2 size=   13954kB time=41.38 bitrate=2762.8kbits/s    
frame= 1261 fps= 31 q=4.6 Lsize=   14428kB time=42.08 bitrate=2809.1kbits/s

推荐答案

是否可以用十六进制编辑器打开文件并检查哪个字符用作框架"行之间的分隔符.我认为在框架行之间可能还存在换行符,这就是为什么尾巴"返回整个块而不是最后一行(在这种情况下恰好是整个框架块)的原因.

Could you maybe open the file with a hex-editor and check which character is used as a separator between the "frame" lines. I think there might be something other than a newline character between the frame lines which is why 'tail' returns the entire block instead of just the last line (which in this case happens to be the entire frame block).

用换行符替换回车符

$line = `sed 's|\\r|\\n|g' "$logfile"| tail -n 1`;

确保如上所述,在传递给sed的正则表达式末尾包含全局开关('g').

Make sure you include the global switch ('g') at the end of the regex passed to sed as shown above.

这篇关于带有PHP尾部的奇数-n 1返回多个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆