tail

How would you implement tail efficiently?

我与影子孤独终老i 提交于 2019-11-28 21:31:47
What is the efficient way to implement tail in *NIX? I came up (wrote) with two simple solution, both using kind of circular buffer to load lines into circular structure (array | doubly linked circular list - for fun). I've seen part of older implementation in busybox and from what I understood, they used fseek to find EOF and then read stuff "backwards". Is there anything cleaner and faster out there? I got asked this on interview and asker did not look satisfied. Thank you in advance. I don't think there are solutions different than "keep the latest N lines while reading forward the data" or

How do I get the last non-empty line of a file using tail in Bash?

╄→гoц情女王★ 提交于 2019-11-28 20:54:24
问题 How do I get the last non-empty line using tail under Bash shell? For example, my_file.txt looks like this: hello hola bonjour (empty line) (empty line) Obviously, if I do tail -n 1 my_file.txt I will get an empty line. In my case I want to get bonjour . How do I do that? 回答1: You can use Awk: awk '/./{line=$0} END{print line}' my_file.txt This solution has the advantage of using just one tool. 回答2: Use tac, so you dont have to read the whole file: tac FILE |egrep -m 1 . 回答3: How about using

How can I follow a file like “Tail -f” does in Java without holding the file open (Prevent rename/delete)

时光总嘲笑我的痴心妄想 提交于 2019-11-28 19:17:01
问题 i'd like to "Tail -f" a lot of logfiles from within a java app. I've gotten this to work by monitoring the size and last update and repeatedly opening the file and reading the last few bytes whenever the file size or last update time changes--then closing it right away. This seems problematic because I might have it open when the logger decides to rename the file which will cause some kind of problem. I'd also like to detect a "Rolled" file with a mechanism more sure than noticing that the

Output file lines from last to first in Bash

∥☆過路亽.° 提交于 2019-11-28 16:51:33
I want to display the last 10 lines of my log file, starting with the last line - like a normal log reader. I thought this would be a variation of the tail command, but I can't find this anywhere. Rick Smith GNU (Linux) uses the following : tail -n 10 <logfile> | tac tail -n 10 <logfile> prints out the last 10 lines of the log file and tac (cat spelled backwards) reverses the order. BSD (OS X) of tail uses the -r option: tail -r -n 10 <logfile> For both cases , you can try the following: if hash tac 2>/dev/null; then tail -n 10 <logfile> | tac; else tail -n 10 -r <logfile>; fi NOTE: The GNU

Printing the last column of a line in a file

喜夏-厌秋 提交于 2019-11-28 15:37:22
I have a file that is constantly being written to/updated. I want to find the last line containing a particular word, then print the last column of that line. The file looks something like this. More A1/B1/C1 lines will be appended to it over time. A1 123 456 B1 234 567 C1 345 678 A1 098 766 B1 987 6545 C1 876 5434 I tried to use tail -f file | grep A1 | awk '{print $NF}' to print the value 766, but nothing is output. Is there a way to do this? You don't see anything, because of buffering. The output is shown, when there are enough lines or end of file is reached. tail -f means wait for more

R: How to get the last element from each group?

佐手、 提交于 2019-11-28 14:20:34
I have a data frame containing a time series with two time stamp columns, d$day and d$time , and say, for simplicity, one measured variable d$val1 . Suppose I want to examine the situation at the close of each day's experiment, i.e. the last measurement, if it exists. (Not every day has a measurement, and measurements can be taken at different times each day.) I would like to be able to aggregate by day and use some sort of last() or tail() function on time to pull back the corresponding val . I've tried variations like this with not much success (one issue is that tail requires an argument,

Implement “tail -f” in C++

荒凉一梦 提交于 2019-11-28 09:50:29
I want to create a small code in C++ with the same functionality as "tail-f": watch for new lines in a text file and show them in the standard output. The idea is to have a thread that monitors the file Is there an easy way to do it without opening and closing the file each time? Just keep reading the file. If the read fails, do nothing. There's no need to repeatedly open and close it. However, you will find it much more efficient to use operating system specific features to monitor the file, should your OS provide them. Have a look at inotify on Linux or kqueue on Mac OS. Inotify is Linux

Tailing Log File and Write results to new file

霸气de小男生 提交于 2019-11-28 07:21:58
问题 I'm not sure how to word this so I'll type it out and then edit and answer any questions that come up.. Currently on my local network device (PHP4 based) I'm using this to tail a live system log file: http://commavee.com/2007/04/13/ajax-logfile-tailer-viewer/ This works well and every 1 second it loads an external page (logfile.php) that does a tail -n 100 logfile.log The script doesn't do any buffering so the results it displayes onscreen are the last 100 lines from the log file. The logfile

python head, tail and backward read by lines of a text file

点点圈 提交于 2019-11-28 04:47:20
How to implement somethig like the 'head' and 'tail' commands in python and backward read by lines of a text file? This is my personal file class ;-) class File(file): """ An helper class for file reading """ def __init__(self, *args, **kwargs): super(File, self).__init__(*args, **kwargs) self.BLOCKSIZE = 4096 def head(self, lines_2find=1): self.seek(0) #Rewind file return [super(File, self).next() for x in xrange(lines_2find)] def tail(self, lines_2find=1): self.seek(0, 2) #Go to end of file bytes_in_file = self.tell() lines_found, total_bytes_scanned = 0, 0 while (lines_2find + 1 > lines

Ending tail -f started in a shell script

邮差的信 提交于 2019-11-28 04:10:00
I have the following. A Java process writing logs to the stdout A shell script starting the Java process Another shell script which executes the previous one and redirects the log I check the log file with the tail -f command for the success message. Even if I have exit 0 in the code I cannot end the tail -f process. Which doesn't let my script to finish. Is there any other way of doing this in Bash? The code looks like the following. function startServer() { touch logfile startJavaprocess > logfile & tail -f logfile | while read line do if echo $line | grep -q 'Started'; then echo 'Server