Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't save text string
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)
   output, err  = p3.communicate()
   return output

out = sysctl_command()
print (out)
and output...
# python ./ <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?

I don't understand why you're not using fileio.
with open('myfilename', 'w') as fp:
Are you piping this to other processes?
Hi Lazr60,

Thank you for your reply and advice. I guess I could pipe the entire output of the FreeBSD command 'sysctl -a' into a file and then parse the file itself. That sounds like a reasonable way to go about it. In my example, I'm basically looking to find out how many NUMA are in the server and how many CPU cores are associated with each NUMA and then use that information ultimately for performance tuning purposes. So, in brief, my script will extract this type of information and then use it for server purpose tuning.

p.s. any idea what I'm doing wrong with my code?

Thanks again!

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to extract multiple text from a string? chatguy 2 493 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  Searching string in file and save next line dani8586 2 662 Jul-10-2020, 09:03 AM
Last Post: dani8586
  the uncompleteness of the text string Cava 2 934 Feb-21-2020, 03:54 PM
Last Post: Cava
  Highlight and remove specific string of text itsalmade 5 1,267 Dec-11-2019, 11:58 PM
Last Post: micseydel
  Converting a patterned string or text into excel table soup1987 1 869 Oct-03-2019, 01:37 AM
Last Post: Larz60+
  Read each line, replace string and save into a new file igormonteiro 2 1,380 Sep-15-2019, 01:24 PM
Last Post: buran
  Save console output to text file (like a log) koticphreak 9 14,203 Jun-02-2019, 07:45 PM
Last Post: snippsat
  write image into string format into text file venkat18 2 1,872 Jun-01-2019, 06:46 AM
Last Post: venkat18
  write each line of a text file into separate text files and save with different names Shameendra 3 1,248 Feb-20-2019, 09:07 AM
Last Post: buran
  Strip does not work when applied on a string read from a text file. susmis666 1 1,160 Dec-27-2018, 07:07 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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