Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't save text string
#1
Hi, I'm working in the FreeBSD OS environment and using Python version 3.6.8.

I'm essentially trying to run a script that does the equivalent of the following FreeBSD command:
sysctl -a | grep "group level=\"2\"" -A 1

So, a sysctl command and then piping it to a grep command. Then, I'd like to parse the remaining output (text/string), capture it (in a variable?) and make decisions on it.

Below is my code where the first subprocess.Popen executes the FreeBSD sysctl command, and I pass the output to "p2" and then "grep it" and pass that to p3 and get the expected output text -- see below.

But, I can't save this text/string to a variable so to parse it further.

So, first here is sysctl run at the FreeBSD command line and the associated output...
# sysctl -a | grep "group level=\"2\"" -A 1
<group level="2" cache-level="3">
<cpu count="8" mask="ff,0,0,0">0, 1, 2, 3, 4, 5, 6, 7</cpu>
--
<group level="2" cache-level="3">
<cpu count="8" mask="ff00,0,0,0">8, 9, 10, 11, 12, 13, 14, 15</cpu>

Then, here's my Python code:

def sysctl_command():
   p1 = subprocess.Popen(['sysctl','-a'], stdout=subprocess.PIPE)
   p2 = subprocess.Popen(['grep', 'group level="2"', "-A 1"], stdin=p1.stdout, stdout=subprocess.PIPE)
   p3 = subprocess.Popen(['grep', 'cpu count'], stdin=p2.stdout)
   p1.stdout.close()
   p2.stdout.close()
   output, err  = p3.communicate()
   return output

#sysctl_command()
out = sysctl_command()
print (out)
and output...
Output:
# python ./mlnx_tune_freebsd.py <cpu count="8" mask="ff,0,0,0">0, 1, 2, 3, 4, 5, 6, 7</cpu> <cpu count="8" mask="ff00,0,0,0">8, 9, 10, 11, 12, 13, 14, 15</cpu> None
You'll notice that I tried to print 'out' variable and get "None" as opposed to the text string that is displayed to the screen.

Any thoughts as so what I might be doing wrong?

Thanks!
Reply


Messages In This Thread
can't save text string - by branko316 - May-29-2020, 05:11 PM
RE: can't save text string - by Larz60+ - May-29-2020, 09:07 PM
RE: can't save text string - by branko316 - May-31-2020, 02:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 554 Oct-23-2023, 10:16 PM
Last Post: deanhystad
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,132 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,232 Jul-07-2023, 05:44 PM
Last Post: flash77
  Editing text between two string from different lines Paqqno 1 1,317 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  Extract a string between 2 words from a text file OscarBoots 2 1,877 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,132 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  fuzzywuzzy search string in text file marfer 9 4,579 Aug-03-2021, 02:41 AM
Last Post: deanhystad
Question How to extract multiple text from a string? chatguy 2 2,372 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  Searching string in file and save next line dani8586 2 2,303 Jul-10-2020, 09:03 AM
Last Post: dani8586
  the uncompleteness of the text string Cava 2 2,192 Feb-21-2020, 03:54 PM
Last Post: Cava

Forum Jump:

User Panel Messages

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