Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract a single word from an os.system command?

I’m writing a python program where I do some os.system(cmd).
I would need to extract one single word from the terminal output. The output contains a seire of informations. Inside this information, I only need the parameter address, as a simple string. How could I do?
This is an example of the ouput:

  --------------------------------
  General            |  dbus path: /org/freedesktop/ModemManager1/Bearer/1
                     |       type: default
  --------------------------------
  Status             |  connected: yes
                     |  suspended: no
                     |  interface: wwp0s20f0u3i2
                     | ip timeout: 20
  --------------------------------
  Properties         |        apn: wap.tim.it
                     |    roaming: allowed
  --------------------------------
  IPv4 configuration |     method: static
                     |    address: 10.200.210.208
                     |     prefix: 27
                     |    gateway: 10.200.210.209
                     |        dns: 217.200.201.65, 217.200.201.64
                     |        mtu: 1500
  --------------------------------
  Statistics         |   duration: 1290

I’ve done:

  proc=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, )
  output=proc.communicate()[0]
  print(output)

but obviously, it returns the entire output. Also including a grep inside the command, isn’t a good solution. The output is bad formatted. I would need:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

10.200.210.208

>Solution :

You might regular expression for that task following way

import re
output = '''  --------------------------------
  General            |  dbus path: /org/freedesktop/ModemManager1/Bearer/1
                     |       type: default
  --------------------------------
  Status             |  connected: yes
                     |  suspended: no
                     |  interface: wwp0s20f0u3i2
                     | ip timeout: 20
  --------------------------------
  Properties         |        apn: wap.tim.it
                     |    roaming: allowed
  --------------------------------
  IPv4 configuration |     method: static
                     |    address: 10.200.210.208
                     |     prefix: 27
                     |    gateway: 10.200.210.209
                     |        dns: 217.200.201.65, 217.200.201.64
                     |        mtu: 1500
  --------------------------------
  Statistics         |   duration: 1290'''
address = re.search("address: ([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+)",output).group(1)
print(address) # 10.200.210.208

Explanation: I use pattern with single capturing group, encased in ( ) which I then access using .group(1)

Disclaimer: I assume address line is always present and address is in form of 4 base-10 numbers sheared by . character.

Note: for brevity I set output to multiline string, rather than calling command.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading