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

  • Home
  • SEARCH
  • 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 6683957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:52:58+00:00 2026-05-26T04:52:58+00:00

I am trying to convert my shell scripts into python code, but I am

  • 0

I am trying to convert my shell scripts into python code, but I am stuck while trying this operation.

I have a process that outputs a text file, the file has sections like this:

Running Operation kdasdakdnaskdaksdma

 (error if present) error: kdmakmdasmdaksom

This file could have multiple lines for the operation and the error (if present, otherwise the next line will just have another operation); there is always a crlf after each block.

I am trying to scan the file to find the line that contains “error:”, and then read the operation that caused the error and the details of the error, so i can extrapolate it from the text file and save it in an error log file.

So far i can find the line(s) that has “error:” in it, with this simple code, but I am not able to find any example about how do you actually print the lines that are not necessarily the ones that contain the error message, but the ones that came before and after the line where “error:” is located.

using awk or grep would be straightforward, but with Python I am not really sure about how to do so; this is what i have so far, that is able to print the line that has the error but it prints just that, while i would like to have control to the lines printed before and after.

import re

fh = open('~/logs_output.txt')

for line in fh:
    if "error:" in line:
        print line

Tried to look at RE module in python, and also to the string modules, but so far I haven’t found anything that would allow me to do what you would do with awk for example, where you can look for an occurrence of a specific string and turn on print, and then turn it off, once that you are done

Can anyone point me to the right direction to tackle this issue? Thanks!

  • 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-05-26T04:52:58+00:00Added an answer on May 26, 2026 at 4:52 am
    import re
    
    
    ss = '''qhvfgbhgozr
    yytuuuyuyuuuyuyuuyy
    jhfg tryy error  jjfkhdjhfjh ttrtr
    aaaeeedddeedaeaeeaeeea
    jhzdgcoiua zfaozifh cohfgdyg fuo'''
    
    regx = re.compile('^(.*)\r?\n(.*?error.*)\r?\n(.*)', re.MULTILINE)
    
    print regx.search(ss).groups()
    

    result

    ('yytuuuyuyuuuyuyuuyy', 'jhfg tryy error  jjfkhdjhfjh ttrtr', 'aaaeeedddeedaeaeeaeeea')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to convert this c code into MIPS and run it in SPIM. int
I'm trying to convert some code that worked great in VB, but I can't
We are trying to convert a bash shell script into a Windows batch script.
I am trying to convert an int into three bytes representing that int (big
I'm trying to convert about 200 lines of my jQuery code into MooTools to
I am trying to convert a standard std::string into an NSString but I'm not
I'm using lambda expression and trying convert into uint while adding into the Hashset.
I have an XML file and I'm trying convert it into a (table( HTML
I am trying to convert some Verilog code that produces a slower clock from
I am trying to convert scheme code to Haskell code. For that, I want

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.