Unix & Linux Asked on November 11, 2021
I have a logfile that is generating hundreds of lines per second – say, 12 specific lines, 16× per sec.
I want to run either a command-line or a shell script that can display this logfile neatly in real-time. But if I run tail -f logfile.txt
, the text rapidly scrolls off the terminal window and can’t be read by human eyes. I haven’t yet mastered the command line so this is all I can think of doing right now.
I want the terminal window to just print 12 lines at a time and automatically refresh, something like:
// while ( Ctrl+C hasn't been hit )
// {
// clear terminal window
// print last 12 lines of logfile.txt
// wait until logfile is 12 lines longer
// }
Any ideas?
EDIT: it turns out I can do tail -f logfile.txt
and just set the terminal window height to 12. This gets me pretty close to what I want, but it seems like a "naive" approach. Hoping somebody has a more elegant solution.
You can view the last N lines at a M second interval using watch
. Assuming N=20 and M=3,
watch -n3 tail -n20 logfile.txt
Obviously you'll lose great chunks of output as the update interval exceeds the write interval, but as far as I understand it this is what you want.
Answered by roaima on November 11, 2021
tail -f logfile.txt | less
And you can use the interface glenn jackman highlighted to move about:
Answered by Scott S on November 11, 2021
I like to use less
for looking at logs. Some useful less commands:
G
)tail -f
): hit Ctrl+C to stop tailing.Answered by glenn jackman on November 11, 2021
Bash >= 4 has a special built-in function mapfile
:
tail -f logfile.txt | while mapfile -t -n 12 lines && ((${#lines[@]})); do
clear
printf '%sn' "${lines[@]}"
sleep 0.1 # Might be necessary or you won't see anything at all except the last batch of lines
done
Adopted from: Read n lines at a time using Bash.
Answered by Artem S. Tashkinov on November 11, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP