Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:57:57+00:00 2026-06-10T04:57:57+00:00

Question: I’m trying to search a syslog (Linux server) for the first entry of

  • 0

Question: I’m trying to search a syslog (Linux server) for the first entry of an specific event and then execute a command (future development) and then log the information. I’m not sure if I have approached this the right way.

Problem: not sure how to exit the loop on the first valid entry found.

import os
import commands
from time import strftime

file = "server.log.1" # sample log
nlogFile =("/home/nsoper/log/logfile_%s.txt") %(strftime("%Y_%m_%d_ %H_%M_%S")) # create logfile
caption="SN" # sample serach
condition2 = "java error"


F=open(file, 'r')
nF=open(nlogFile, 'a')
nF.write("output for results search:\n\n")


print(strftime("%Y_%m_%d_ %H_%M_%S"))


for line in F:
       for item in line.split("\n"):
                 if caption in item:
                        line1=item.strip()
                        if condition2 in line1:
                                print line1.strip()
                                nF.write(line1.strip()+"\n")
                                #command execute


nF.close()
F.close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:57:59+00:00Added an answer on June 10, 2026 at 4:57 am

    Here is the best way that I found to get this accomplished using python 2.4 (Linux prod env server. Not able/feaisble to upgrade python.) In this specific case I wanted to grab the last line, and then process it using another loop.

    index=[]
    con1 = "First Condition"
    con2 = "Second Condition"
    
    file=open("log.txt",'r')
    
    print("Reading.. "+ file)
    for line in F:
        for item in line.split("\n"):
                if con1 in item:
                        con1Line=item.strip()
                        if con2 in con1Line:
                                index.append(con1Line)
    F.close()
    l=index[-1]
    print(l)
    # do stuff now will value l
    del index[:]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

QUESTION: What am I missing or doing wrong? I'm trying to migrate fully functional
Question is : how to add any event in calender? i want to make
Question for the etymology wizards out there: which programming language was the first to
Question 1 I am building/searching for a RAM memory cache layer for my server.
Question What's the full code to enable jQuery on a page and then use
Question's pretty self explanatory. I trying to get the horizontal motion of my 15px
Question in Brief* When raising an event in a top level master page through
Question: Normally, you can undo a sql command with rollback. BEGIN TRY BEGIN TRANSACTION
Question Is it possible to stop a Message Driven Bean (programmatically), so that it
Question context: let say that there is some really important row in config/locales/en.yml that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.