LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-06-2009, 09:24 AM   #1
andrew22
Member
 
Registered: Jan 2009
Location: Heaven
Distribution: Ubuntu 8.10 , openSUSE 11.1
Posts: 56

Rep: Reputation: 16
Question "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.

Last edited by andrew22; 02-06-2009 at 09:25 AM. Reason: Some additions.
 
Old 02-06-2009, 09:27 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
tail -1 temperature_log
 
Old 02-06-2009, 09:29 AM   #3
jqbsx
LQ Newbie
 
Registered: Oct 2004
Posts: 7

Rep: Reputation: 0
use: tail -n XX file
 
Old 02-06-2009, 09:39 AM   #4
andrew22
Member
 
Registered: Jan 2009
Location: Heaven
Distribution: Ubuntu 8.10 , openSUSE 11.1
Posts: 56

Original Poster
Rep: Reputation: 16
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"

Last edited by andrew22; 02-06-2009 at 09:49 AM. Reason: Added the new script
 
Old 02-06-2009, 09:41 AM   #5
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
slow night at work!
 
Old 02-06-2009, 04:08 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
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;

Last edited by ntubski; 02-06-2009 at 04:08 PM. Reason: can't spell temperature
 
Old 02-06-2009, 04:58 PM   #7
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Quote:
Originally Posted by ntubski View Post
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".
 
Old 02-07-2009, 07:00 AM   #8
shyamkumar1986
LQ Newbie
 
Registered: Feb 2009
Posts: 19

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

Last edited by shyamkumar1986; 02-09-2009 at 09:38 AM.
 
Old 02-08-2009, 06:56 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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 ...'
 
Old 02-08-2009, 07:14 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,488

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
Doesn't keep the file open ...
 
  


Reply

Tags
binbash, cat, last, line, log, temperature


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 07:58 AM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 08:27 AM
Stupid question: if [ "$i" == `$(cat ${LOGFILESSHD} | grep "${i}" )` ] ; then frenchn00b Programming 6 05-19-2008 06:16 PM
"cat" a file without first line loopoo Linux - General 5 12-05-2005 08:04 AM
cdrecord command-line parentheses "(" ")" coolingtower Linux - Software 1 10-28-2005 12:49 PM


All times are GMT -5. The time now is 08:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration