Reputation: 407
I use the command the grep file content, and do something.
However, the file size is growing continuous every second. (will larger than 500MB)
Due to the performance issue, I want to grep file content in last N lines rather than
entire file content.
if grep -q "SOMETHING" "/home/andy/log/1.log"; then
ps -ef | grep "127.0.0.1:50000" | awk '{print $2}' | xargs kill; cat /dev/null > /home/andy/log/1.log
fi
How can I modify the script to grep file content in last N lines?
Thanks!
Upvotes: 7
Views: 19937
Reputation: 246774
Another approach is to compare the file size now versus the past and use tail -c
to get the difference in characters (bytes)
# prev_size is known
curr_size=$(stat -c %s file.log)
if tail -c $((curr_size - prev_size)) | grep -q pattern; then ...
# ...
fi
prev_size=$curr_size
# loop
This way you're more certain that you're not missing anything, or grepping too much.
Upvotes: 2
Reputation: 4396
you can use tail -n to get the last n lines of a file.
So you if you wanted to look only at the last 100 lines you could make your script work like this:
if tail -n 100 "/home/andy/log/1.log" | grep -q "SOMETHING"; then
...
Upvotes: 18