35

I am trying to find a Unix command (combination, maybe) on how to continuously display a file of its last several lines of contents. But during this displaying, I want some of the top lines are always displayed on the screen top when the rolling contents reach the screen top.

Is that possible?

  1. Suppose I have file, "job.sta", the first 2 lines are:
    job name, John's job on 2013-Jan-30,...
    Tab1, Tab2, Tab3
    0, 1, 2,
    1, 90, 89
    2, 89, 23
    ...
    
  2. This file is on its running, its contents are growing, and I don't know what line it's going to end.
  3. So I want to display (always) the first 2 lines when using tail command, when the updating contents reaches a Unix shell screen top. I am using PuTTY at the moment.

Reference: http://www.unix.com/unix-dummies-questions-answers/172000-head-tail-how-display-middle-lines.html

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Wen_CSE
  • 1,225
  • 2
  • 10
  • 8
  • 1
    I don't think this could be done by simple shell commands. correct me if I am wrong. You are asking split the screen into two parts, above part shows the fixed 2 lines (newly first added 2 lines in that session), and down part shows sth like tail -f... it would look a bit like top command.. not in place changing but scrolling. – Kent Jan 30 '13 at 12:58
  • Related: [Unix & Linux: How do I "cat and follow" a file?](https://unix.stackexchange.com/questions/139866/how-do-i-cat-and-follow-a-file) – Gabriel Staples Jan 20 '22 at 19:46

6 Answers6

73

I use this function all the time to monitor a log file in another terminal window.

tail -f <filename>

I recommend taking it a step forward to look for particular text in the log. Great if you are only interested in seeing some particular entry being written to the file.

tail -f <filename> | grep <keyword or pattern>
wacko413
  • 731
  • 1
  • 5
  • 3
31

This will update every 2 seconds rather than whenever data is written to the file, but perhaps that's adequate:

watch 'head -n 2 job.sta; tail job.sta'
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 1
    I tried and tested this is the best answer to suit my purpose. Many thanks. Also thanks to all the other responses. I also much appreciate your help. – Wen_CSE Jan 31 '13 at 10:00
  • 2
    Hi, Bro, I've got a new question based on your best answer. How can I write a customized command, like watch2. It runs as follows: $>watch2 job.sta So, it will get the filename from the command line arguments. I've tried much. But I am so pissed by the quote symbols. Do you have any light to shed? – Wen_CSE Feb 05 '13 at 09:37
  • 4
    `watch2() { head -n 2 "$1"; tail "$1"; }` – William Pursell Feb 07 '13 at 01:10
  • I just love SE! – lineage Feb 11 '22 at 13:56
8

You can use screen to simulate the expected behaviour:

  1. Run screen, press Space.

  2. Press Ctrl+a followed by S to split the screen.

  3. Resize the top window by pressing Ctrl+a followed by :resize 4.

  4. In the prompt in the top window, enter head -n2 file.

  5. Move to the bottom window by pressing Ctrl+a followed by Tab.

  6. Start a new screen session by pressing Ctrl+a followed by c.

  7. In the new prompt, enter tail -f file.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • 1
    This method seems to be also a solution although there are quite several steps. I also like it! Thank you! – Wen_CSE Feb 05 '13 at 09:33
4

I use this script to achieve the effect you describe

!#/bin/bash
while [ 1 ]; do ls -lt data | head -n 30; sleep 3; echo -en "$(tput cuu 30; tput ed)"; done

This runs a command in a loop, and deletes the last lines of the output from the screen before each iteration.

In your case it would look something like

!#/bin/bash

while [ 1 ] ;# loop forever
do 
    head -n 2 job.sta ;# display first 2 lines 
    tail -n 30 job.sta ;# display last 30 lines from file
    sleep 3 ;# wait a bit
    echo -en "$(tput cuu 32; tput ed)" ;# delete last 32 lines from screen
done

Of course it is a bit ugly before your file reaches 30 lines

Hope that helps

user000001
  • 32,226
  • 12
  • 81
  • 108
2

You want the multitail program, which not only does the split screen stuff but will also do color-coding.

http://www.vanheusden.com/multitail/

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
1

You try the following combination tail -f filename | head -2

sameer karjatkar
  • 2,017
  • 4
  • 23
  • 43
  • 1
    no it is not what OP wants " I want some of the top lines are always displayed on the screen top when the rolling contents reach the screen top." – Kent Jan 30 '13 at 12:54