zoukankan      html  css  js  c++  java
  • 使用python脚本实现基于指定字符串的文本排序

    朋友用ansible导出了一个文件,文件中包含上千台机器的磁盘信息,他想要知道哪些机器最需要赶紧扩磁盘。思路是,按剩余磁盘空间百分数,从小到大对文本内容重新排序。下面是具体实现。

    源文件ip.txt的格式是这样的:

    10.1.2.1 224 | success | rc=0 >>
    /dev/sda1  1.1T  28G  982G  3%  /export
    
    10.1.2.1 24 | success | rc=0 >>
    /dev/sda2  1.6T  15G  1.5T  1%  /export

    下面是.py文件。运行这个python脚本后,可以生成ip_sorted.txt文件,是经过重排的文件,磁盘剩余空间越小,其机器信息会排在越前面。

    #!/bin/bash
    # -*- coding:utf-8 -*-
    
    import os
    import collections
    
    with open('ip.txt','r') as f:
        file_dic_ip = {}
        file_dic_info = {}
        i = 0
        for line in f:
            count = int(os.popen("grep -c '^/dev' ip.txt").read())
            while i < count:
                lines_ip = os.popen("grep -v '^/dev' ip.txt | grep -v '^$' ").read().split('
    ')
                lines_info = os.popen("grep -n '^/dev' ip.txt | awk -F ':' '{print $2}'").read().split('
    ')
                file_dic_ip[lines_info[i]] = lines_ip[i]
                lines_info_percent = os.popen("grep -n '^/dev' ip.txt | awk -F ' ' '{print $5}'").read().split('%
    ')
                file_dic_info[lines_info_percent[i]] = lines_info[i]
                i += 1
    
    od = collections.OrderedDict(sorted(file_dic_info.items()))
    with open('ip_sorted.txt','w') as f:
        for items in od.items():
            f.write(file_dic_ip[items[1]] + '
    ' + items[1] + '
    
    ')

    流程图不太对,将就看看文字讲解吧

  • 相关阅读:
    Java 1.7.0_21b11 Code Execution
    nginx NULLByte 任意代码执行漏洞
    nginx ‘ngx_http_close_connection()’远程整数溢出漏洞
    WordPress WP Super Cache插件任意代码执行漏洞
    memcached 远程拒绝服务漏洞
    原环套原环
    要去哈尔滨了
    母亲节就要到了,你忘了吗?
    对于流媒体服务的一点概念
    有了螃蟹让心情好一点
  • 原文地址:https://www.cnblogs.com/huamingao/p/5766788.html
Copyright © 2011-2022 走看看