LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "cat" only the last line. how? (https://www.linuxquestions.org/questions/programming-9/cat-only-the-last-line-how-702728/)

andrew22 02-06-2009 08:24 AM

"cat" only the last line. how?
 
I want to check a log into terminal, the logger writes a temperature_log in /var/log . How can I <<cat>> only the last line of the log, so I can append it to the output?
Here is the bad code:
Code:

#! /bin/bash

cd /var/log
x=0
until [ x = 1 ]; do
echo "`cat temperature_log`" #Thats the bad section
sleep 5
done
echo "Error!"

I wanted to do sommething like this:
Code:

echo "`cat_last_line temperature_log`"
But unfortunately , "cat_last_line" is yet to be invented,
any suggestions?
Edit: the logger updates the log every 5 seconds , thats the "sleep 5" for.

JulianTosh 02-06-2009 08:27 AM

tail -1 temperature_log

jqbsx 02-06-2009 08:29 AM

use: tail -n XX file

andrew22 02-06-2009 08:39 AM

5 minutes and 2 answers , thanks a lot.
Thats the new script:
Code:

#! /bin/bash

cd /var/log
x=0
tail -100 temperature_log #Makes it more flexible and fast than "cat"
until [ x = 1 ]; do
tail -1 temperature_log #Thats the whole deal!!!
sleep 5
done
echo "Error"


JulianTosh 02-06-2009 08:41 AM

slow night at work! ;)

ntubski 02-06-2009 03:08 PM

I think this script might equivalent to
Code:

tail -f /var/log/temperature_log
Code:

~$ man tail
...
      -f, --follow[={name|descriptor}]
              output appended data as the file grows;


JulianTosh 02-06-2009 03:58 PM

Quote:

Originally Posted by ntubski (Post 3434523)
I think this script might equivalent to
Code:

tail -f /var/log/temperature_log
Code:

~$ man tail
...
      -f, --follow[={name|descriptor}]
              output appended data as the file grows;


Not if andrew wants to check the log file every 5 seconds... The -f will not release the script until the user hits ctrl-c. "tail -1" simply outputs the last line of the file and quits so the rest of the script can do it's thing, including "sleep".

shyamkumar1986 02-07-2009 06:00 AM

In that case - you might want to do tail -f 1 "file_name" & instead

chrism01 02-08-2009 05:56 PM

Re post #4
Code:

until [ x = 1 ]; do
tail -1 temperature_log #Thats the whole deal!!!
sleep 5
done

is an infinite loop, so it does look like 'tail -f ...'

syg00 02-08-2009 06:14 PM

Doesn't keep the file open ...


All times are GMT -5. The time now is 09:18 PM.