Python Forum
Capture grep output to a variable which includes another variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Capture grep output to a variable which includes another variable
#1
I have set a variable strFileExt to a file path/name: /var/log/error.log.112119

I would now like to grep that file, capturing output to a second variable. If there is no output (text does not exist) I will exit. If it does return output, I will then move on to perform other things with this file.

I have tried many variations of the following to no success:

strFile = os.popen('grep -rlw "reported errors in the" + strFileExt+').read()
Is there a way to set this strFile variable using the grep command which includes the first set variable of strFileExt ?

Thanks in advance.
Reply
#2
I would suggest to use subprocess.run command, e.g.

import subprocess
what = "what"
where = "/home/yourfolder/yourfile"
p = subprocess.run(["grep", what, where], capture_output=True)   
p.stdout  # is what you need
Reply
#3
Scidam, that looked promising, but getting error. Also, had to change subprocess.run to subprocess.call due to being on Python 2.7.

Here's my code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

strFileExt=""
strFile=""


# import required modules
import datetime
import os
import subprocess
now = datetime.datetime.now()

# for testing timie format
# print (now.strftime(("%m%d%y"))
#print (now.strftime("%m%d%y"))
strFileExt = "/var/logs/error.log."+(now.strftime("%m%d%y"))
print strFileExt

what = " reported errors in the "
where = strFileExt
print what
print where

p = subprocess.call(["grep -rlw ", what, where])
strFile=p.stdout
print strFile
whether I use p.stdout or try to stuff it in a variable as above, the error is the same, shown below:

/var/logs/error.log.112219
 reported errors in the
/var/logs/error.log.112219
Traceback (most recent call last):
  File "./integ_recheck.py3", line 25, in <module>
    p = subprocess.call(["grep -rlw ", what, where])
  File "/usr/lib64/python2.7/subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
Reply
#4
(Nov-22-2019, 02:22 PM)kdefilip2 Wrote: Scidam, that looked promising, but getting error.
You should split each command and pass these commands as a list, e.g.
p = subprocess.call(["grep", "-rlw", what, where])
Reply
#5
Hi Scidam,

Thanks for all your help. The only way I could get this working was with the following:
p = Popen(["grep", "-wl", what, where], stdout=True, stderr=True)
p.communicate()[0]
It now returns proper grep output, i.e., when found it returns the filename, when not found, it returns nothing.

I now need to find a way to compare the result:
IF Found
do a bunch of other stuff
ELSE
Exit

But I guess that will be for another post.

Thanks again.

kd
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions: why use a variable "display" in a UDF as a Boolean MMartin71 3 346 May-26-2025, 05:55 AM
Last Post: DeaD_EyE
  PYTHONHOME Variable correct setting msetzerii 0 206 May-25-2025, 11:48 PM
Last Post: msetzerii
  how to get variable Azdaghost 3 609 Apr-23-2025, 07:43 PM
Last Post: deanhystad
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 378 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 744 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 1,164 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
  creating arbitrary local variable names Skaperen 9 2,113 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  Variable Substitution call keys Bobbee 15 3,210 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  how solve: local variable referenced before assignment ? trix 5 1,955 Jun-15-2024, 07:15 PM
Last Post: trix
  Variable being erased inside of if statement deusablutum 8 2,409 Jun-15-2024, 07:00 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020