-1

I have an output file which is constantly overwritten. There is a command in terminal tail -f filename which is helpful when the results are appended in the output file. However, I want to observe the first 10 or 20 lines of the constantly overwritten output file. Is there any such command?

3 Answers3

1

It refreshes every 1 second and prints first 100 lines of filename

watch -n 1 head -100 filename
yausername
  • 750
  • 5
  • 15
0

If using FreeBSD you would use wait_on, for example:

#!/bin/sh

while :; do
    tail file
    wait_on -w file
done

You may need to install it by using pkg install wait_on

In Linux you could use inotifywait something like:

#!/bin/sh

inotifywait --quiet --monitor --event modify file | while read; do
   tail file;
done

Based on your distro you may need to install the inotify-tools package, for example in CentOS: yum install inotify-tools

For more options check the answer to this quesion: https://superuser.com/q/181517/284722

nbari
  • 25,603
  • 10
  • 76
  • 131
0

Suggestion using php.

In the command line. This won't load the whole file in memory.

Output first 8192bytes of seed.txt.

php -r "echo fread(fopen('seed.txt','r'), 8192);"

fread() reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met http://php.net/manual/en/function.fread.php

See https://stackoverflow.com/a/15025877/2494754

NVRM
  • 11,480
  • 1
  • 88
  • 87