Oddity with PHP tail -n 1 returning multiple results

雨燕双飞 提交于 2019-12-12 16:34:30

问题


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.

Let me show you....

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"`;

RETURNS...

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

回答1:


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).

Substitute the carriage return with a newline as so

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

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




回答2:


Those lines are seperated by "\r", i.e. a carriage return, not a line feed, because ffmpeg (that's what you're using, right?) wants to show them on the same line of the console. Tail, however, expects "\n" as a line seperator.

To summarize our little comment chat below: Calling

$line = `sed -e "s/\\r/\\n/g" $file | tail -n 1`

will replace the carriage returns by line feeds before calling tail, thus giving the expected results.



来源:https://stackoverflow.com/questions/1062896/oddity-with-php-tail-n-1-returning-multiple-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!