user54
user54

Reputation: 583

Sort stdout by IP address in Python

I have several methods which are being executed in parallel by using multiprocessing module but do not show output in correct order. For example:

Host:10.76.0.114  Running upon kernel reinstallation for GRUB
Host:10.76.0.114  Running: /sbin/new-kernel-pkg --package kernel --mkinitrd --dracut --depmod --install --multiboot=/boot/xen.gz 3.18.34-20.el6.x86_64

Host:10.76.0.121  Running %pre for the backup-tools
Host:10.76.0.112  Setting up Update Process
Host:10.76.0.121  Running: yum -y -q  update httpd

When I need something like:

Host:10.76.0.112  Setting up Update Process
Host:10.76.0.114  Running upon kernel reinstallation for GRUB
Host:10.76.0.114  Running: /sbin/new-kernel-pkg --package kernel --mkinitrd 
Host:10.76.0.121  Running %pre for the backup-tools
Host:10.76.0.121  Running: yum -y -q  update httpd

I was trying to created it using test script but having issues with it:

import StringIO
import string, sys

stdout = sys.stdout

sys.stdout = file = StringIO.StringIO()

print """
10.76.0.114  Initiate Recovery images download
10.76.0.114  Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1
10.76.0.114  2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install
10.76.0.114
10.76.0.113  Preparing...                ########################################### [100%]
10.76.0.113  package 3-2.noarch is already installed


"""

sys.stdout = stdout

l=file.getvalue()
#print l
l=str(l)
l=l.split()
#print l
print sorted(l, key=lambda k:k[0])

I get the next output:

['###########################################', "'4.2'", "'4.2'", '-b', '-N', '-i', '-o', '-P', '/tools/recovery/recovery-templates-url.list', '/tools/recovery/recovery-templates-download.log', '/tools/recovery', '00:33', '10.76.0.114', '10.76.0.114', '10.76.0.114', '10.76.0.114', '10.76.0.113', '10.76.0.113', '10.76.0.113', '2>&1', '2016-06-30', '>/dev/null', 'Finished', 'Hypervisor', 'Initiate', 'Preparing...', 'Recovery', 'Running:', 'Retrieving', 'StorageAPI', 'Xen', '[100%]', 'and', 'already', 'download', 'images', 'install', 'is', 'installed', , 'package', 'wget'

How can I sort by IP address in this case in order to obtain correct output which I mentioned above ?

Please help!

Upvotes: 0

Views: 118

Answers (1)

Devi Prasad Khatua
Devi Prasad Khatua

Reputation: 1235

Here I have modified some:

import StringIO
import string, sys

stdout = sys.stdout

sys.stdout = file = StringIO.StringIO()

print """
10.76.0.114  Initiate Recovery images download
10.76.0.114  Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1
10.76.0.114  2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install
10.76.0.114
10.76.0.113  Preparing...                ########################################### [100%]
10.76.0.113  package 3-2.noarch is already installed


"""

sys.stdout = stdout

text = file.getvalue()
lines = [line for line in text.splitlines() if line]
print sorted(lines, key=lambda line: line.split(' ')[0])  

Here's how the o/p will look like:

>>> for sorted_line in sorted(lines, key=lambda line: line.split(' ')[0]):
...     print sorted_line
10.76.0.113  Preparing...                ########################################### [100%]
10.76.0.113  package 3-2.noarch is already installed
10.76.0.114  Initiate Recovery images download
10.76.0.114  Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1
10.76.0.114  2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install
10.76.0.114

Upvotes: 2

Related Questions