LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-12-2015, 10:46 AM   #1
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 26

Rep: Reputation: Disabled
Python script to read and print all new messages in a file


Hi All,

Basically im trying to read any new line entries in a log file and then print them such as the below ( i know how to read an entire log file however only want the new updates)

print 'new line', new ## new being the variable for any new lines

basically in bash
tail -f logfile | while read new ; do echo "new line $new" ; done

any help and explanation would be most appreciated.

Thanks in advance
 
Old 11-12-2015, 11:18 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Well the obvious one would be to still use tail, but I would guess you want to use only python? If yes, then you would need to present your script with a time (assuming the log has a time column)
and based on it only show lines with a time greater than that. Is this what you are looking for?
 
Old 11-13-2015, 04:56 AM   #3
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Well the obvious one would be to still use tail, but I would guess you want to use only python? If yes, then you would need to present your script with a time (assuming the log has a time column)
and based on it only show lines with a time greater than that. Is this what you are looking for?
Hey,

Yeah i would like to stick to python i know my way around bash fine just starting down the python road now

My script below

Code:
#Open file and read
f=open("/var/log/messages")
##scan through file 
for line in f:
        if "SIGTERM" in line: 
                print 'match', line 
f.close()
so ideally rather than scan through the file i want to simply check new lines
 
Old 11-13-2015, 08:09 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,504

Rep: Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434
what do you mean by new ?
 
Old 11-13-2015, 08:19 AM   #5
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
what do you mean by new ?
by new i mean any new lines added to a file. IE the same thing tail -f does.

i dont want to know about whats already in the file i want to know about whats appended to it while im reading it. (if that makes sense, only new messages)
 
Old 11-13-2015, 08:42 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,504

Rep: Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434
probably this will give you some tips: http://stackoverflow.com/questions/7...-remote-tail-f
 
  


Reply


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
[SOLVED] File /var/adm/messages cannot be read. prathamesh7478 Linux - Newbie 10 03-01-2013 01:51 PM
read a line from file , parse it and store in variables,print them using shell script swapnika.a60 Linux - Newbie 2 12-20-2012 08:20 AM
[SOLVED] How to read an external file and print it with a definite format within AWK script tia_chofi Linux - Newbie 2 12-13-2011 05:26 AM
[SOLVED] Need python script to create .menu file (recursively read directories) tonyfreeman Programming 1 11-05-2010 01:31 PM
python read output from seperate script? bendeco13 Programming 1 02-01-2005 11:38 PM


All times are GMT -5. The time now is 01:15 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration