在Python中按IP地址排序标准输出

用户54

我有几种方法正在通过使用多处理模块并行执行,但没有以正确的顺序显示输出。例如:

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

当我需要类似的东西时:

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

我试图使用测试脚本创建它,但是出现问题:

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])

我得到下一个输出:

['###########################################', "'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'

在这种情况下,如何按IP地址排序以获得我上面提到的正确输出?

请帮忙!

Wolframalpha

在这里我修改了一些:

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])  

o / p如下所示:

>>> 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

输出中的IP地址“ @”(在)

如何在python中对ip地址的元组进行排序/排序

按IP地址对Excel列进行排序

使用标准命令行工具解析命令输出中的所有IP地址

Python按三个标准排序

使用Python脚本对IP地址进行排序

Python从telnetlib输出获取IP地址

在go中按域查找IP地址

检查IP地址是否在范围/子网中的标准/安全方式

按ip格式排序的CSV格式的nmap输出

按 IP 地址循环

Unix-计算唯一的IP地址,按最常见的IP地址排序,并在重复次数相同的情况下按IP排序

Python中的JSON输出排序

如何将IP地址替换为列表中的字符串,以通过检查IP地址之间的重复来对python中的字符串进行排序

清除/覆盖Python中的标准输出

在Python中,测试函数的标准输出

按列排序输出

Perltidy始终按标准输出

Powershell对txt文件中的IP地址进行排序

如何对在Oracle中存储为varchar的IP地址进行排序

在输出文件中显示客户端的IP地址?

如何从traceroute输出中删除IP地址周围的方括号()

Linux 在 1 行输出中打印多个 IP 地址 (ifconfig)

如何在Wireshark中按IP地址过滤?

在Python中管道输出标准输出时设置正确的编码

txt文件中的python ping ip地址

在Python列表中的IP地址的最后一个八位位组上排序

如何对输出进行排序,其中每个 IP 地址在 PowerShell 中都有最大值?

Ansible-按最后一个参数(IP地址)对命令列表进行排序