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!
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
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With