LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 08: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 08:25 AM. Reason: Some additions.
 
Old 02-06-2009, 08: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, 08:29 AM   #3
jqbsx
LQ Newbie
 
Registered: Oct 2004
Posts: 7

Rep: Reputation: 0
use: tail -n XX file
 
Old 02-06-2009, 08: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 08:49 AM. Reason: Added the new script
 
Old 02-06-2009, 08: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, 03:08 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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 03:08 PM. Reason: can't spell temperature
 
Old 02-06-2009, 03: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, 06: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 08:38 AM.
 
Old 02-08-2009, 05:56 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 06:14 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

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


Reply

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



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 06:58 AM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM
Stupid question: if [ "$i" == `$(cat ${LOGFILESSHD} | grep "${i}" )` ] ; then frenchn00b Programming 6 05-19-2008 05:16 PM
"cat" a file without first line loopoo Linux - General 5 12-05-2005 07:04 AM
cdrecord command-line parentheses "(" ")" coolingtower Linux - Software 1 10-28-2005 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:11 PM.

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
Open Source Consulting | Domain Registration