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?
Asked
Active
Viewed 121 times
-1
-
do you want a refresh every given period or only if the file has changed – kyodev Apr 05 '18 at 17:12
-
Do you want or need any guarantee that you will read the file at least once between each write? – chepner Apr 05 '18 at 17:27
3 Answers
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

NVRM
- 11,480
- 1
- 88
- 87