正则处理脚本

 

替换老cmd,输出新conf文件:


要求:
1.	输入三个文件 a2e /a2r/ q2e
对 "cmd" : { }中内容进行操作
   "lrm" : {
        "grp" : 2,
        "dev" : 201,
        "grp_cfg" : "../../group/group_reg.cfg",
        "cmd" : { 
            "a2r_b_bank_checkin_req"                      : {"type" : 129, "grps":[23, 16]},
"reg_flow_nty"                              : {"type" : 129, " grps " : [ 4 ,  16 ] },
                "a2r_wm_ch_trader_pwd_man_req" : {
                    "type" : 133,
                    "grps":[2]
           },
        },
        "line_cfg" : {
            "client" : []
        }
    },
2.cmd名如果存在于csv.txt的第一列,则替换为第三列。不存在则整行不需要输出。
3.如果替换后的cmd名重复,则把grps数组内的组号合并,不能重复。
如:
"qte_inst_info_nty":{"type" : 129,"grps":[15, 20]},
"qte_inst_info_nty":    {"type":129,"grps":[8,10,4,5,20,]},
合并后	
"qte_inst_info_nty":    {"type":129,"grps":[8,10,4,5,20,15]},
4.来源哪个文件则在第一行加上注释
如:
#qte
"qte_reg_seat_capital_frz_req":       {"type":129,"grps":[11,20,]}
#a2r
"a2r_b_bank_checkin_req"                      : {"type" : 129, "grps":[23,16]},
5. “cmd”:{}内容应满足json格式 
6.替换“cmd”:{}后输出完整的新conf文件

文件内容类似:

{
    "read_thread_num" : 1,
    "write_thread_num" : 1,
    "gen_core" : 1,
    "log" : {
        "name" : "./log/smlt_gems",
        "level" : 0
    },
    "pool" : {
        "pool_type" : 6,
        "listen_port" : 3021,
        "backlog" : 500,
        "auth_ip" : ["127.0.0.1", {"0.0.0.0" : "255.255.255.255"}],
        "max_fds" : 1024,
        "works_num" : 1,
        "poll_timeout" : 500
    },
    "web_pool" : {
        "pool_type" : 6,
         "listen_ports":{
             10054:1
         },
        "backlog" : 500,
        "auth_ip" : ["127.0.0.1", {"0.0.0.0" : "255.255.255.255"}],
        "max_fds" : 1024,
        "works_num" : 1,
        "poll_timeout" : 500,
        "read_thread_num" : 1,
        "write_thread_num" : 1
    },
    "protocol" : {
        "read_timeout" : 1000,
        "write_timeout" : 1000,
        "connect_timeout" : 1000,
        "type" : 2
    },
    "web_protocol" : {
        "read_timeout" : 1000,
        "write_timeout" : 1000,
        "connect_timeout" : 1000,
        "type" : 5
    },
    "lrm" : {
        "grp" : 2,
        "dev" : 201,
        "grp_cfg" : "../../group/group_reg.cfg",
        "cmd" : { 
            "a2e_szse_add_cut_qry_req": {"type" : 1, "grps" : [8], "etf_record" : 0 },
            "a2e_sse_biz_over_req": { "type" : 1, "grps" : [7], "etf_record" : 0 }
        },
        "line_cfg" : {
            "client" : []
        }
    },
    "plugin" : {
        "plugin_dir" : "./",

        "plugin_config" : {}
    },
    "py_path": "../../py/lib",
    "auto_reply" : "./conf/auto_reply.cfg"
}

 写的代码如下:

import re
import json
import copy


def read_cmd_from_file(file_name_list):
    '''
    从文件列表中读取cmd
    :param file_name_list: 文件列表
    :return:
    '''
    cmd_list = []
    for file in file_name_list[:]:
        with open(file, "r") as f:
            data = f.read()
            cmd_data = re.search('"cmd" : (\{[\s\S]*\}),[\s\S]*\"line_cfg\"', data).group(1)
            if re.search("\},[\s\S]*?\},", cmd_data):
                cmd_data = re.sub("(?<=}),(?=[\s\t\n]*\})", "", cmd_data)
            if re.search("\[.*?\]", cmd_data):
                cmd_data = re.sub("\[", "\"[", cmd_data)
                cmd_data = re.sub("\]", "]\"", cmd_data)
            if re.search(",(?=\})", cmd_data):
                cmd_data = re.sub(",(?=\})", "", cmd_data)

            if "q2e" in file:
                cmd_json = cmd_remove_duplicate(cmd_data)
            else:
                cmd_json = json.loads(cmd_data)
                for key in cmd_json:
                    if "grps" in cmd_json[key]:
                        cmd_json[key]["grps"] = eval(cmd_json[key]["grps"])
                    if " grps " in cmd_json[key]:
                        cmd_json[key]["grps"] = eval(cmd_json[key].pop(" grps "))
        cmd_list.append({file: cmd_json})
    return cmd_list


def cmd_remove_duplicate(cmd_string):
    '''
    将cmd的json字符串中的key值去重后,返回json
    :param cmd_string: 字符串
    :return: json
    '''
    tmp_list = []
    for line in cmd_string.split("\n"):
        if re.search("\".*\":", line.strip()):
            key = re.search("\"(.*?)\"", line).group(1)
            value = re.search("({.*?})", line).group(1)
            tmp_list.append((key, value))
    res_dict = {}
    for item in tmp_list:
        sub_dict = json.loads(item[1])
        if "grps" in sub_dict:
            sub_dict["grps"] = eval(sub_dict["grps"])
        if item[0] not in res_dict:
            res_dict[item[0]] = sub_dict
        else:
            res_dict[item[0]]["grps"] = list(set(res_dict[item[0]]["grps"] + sub_dict["grps"]))
    return res_dict


def switch_cmd(cmd_list, file_name):
    '''
    对cmd进行转换
    :param cmd_list: cmd列表 [{"文件名": {cmd}}]
    :param file_name: 文件名,存放替换的cmd名称
    :return:
    '''
    with open(file_name, "r") as f:
        data = f.readlines()
    res_list = copy.deepcopy(cmd_list)
    for index in range(len(cmd_list)):
        key = list(cmd_list[index].keys())[0]
        cmd_dict = cmd_list[index][key]
        for cmd_dict_key in cmd_dict:
            for item in data:
                if item.split(',')[0] == cmd_dict_key:
                    res_list[index][key][item.split(',')[2].strip()] = res_list[index][key].pop(cmd_dict_key)
    return res_list


def merge(json_list):
    '''
    合并cmd
    :param json_list: [{"文件名": {}}, {}, {}]
    :return:
    '''
    res = {}
    for item in json_list:
        for file_key, json_value in item.items():
            for key, value in json_value.items():
                if key not in res:
                    res[key] = value
                else:
                    tmp = copy.deepcopy(res[key])
                    res[key] = dict(res[key], **value)
                    if 'grps' in tmp and 'grps' in value:
                        res[key]['grps'] = list(set(tmp['grps'] + value['grps']))

    return res


def read_rest_content(file):
    '''
    读取剩下的内容
    :param file: 文件名
    :return: list
    '''
    res = []
    index = 0
    with open(file, 'r') as f:
        count = -1
        while True:
            line = f.readline()
            count += 1
            if "cmd" in line:
                while True:
                    line = f.readline()
                    if "line_cfg" in line:
                        res.append(line)
                        index = count
                        break
            else:
                res.append(line)
            if not line:
                break
    return res, index


def run(file_name_list):

    rest_content, insert_index = read_rest_content(file_name_list[0])
    source_list = read_cmd_from_file(file_name_list)
    switch_list = switch_cmd(source_list, "csv.txt")
    res_dict = merge(switch_list)
    a2e_dict = switch_list[0]
    a2r_dict = switch_list[1]
    q2e_dict = switch_list[2]
    big_dict = dict(a2e_dict, **a2r_dict, **q2e_dict)

    tmp_write_list = re.findall("(\'.*?\})", str(res_dict), 0)
    tmp_list = []
    for tmp in tmp_write_list:
        key = re.search("\'(.*?)\':", tmp).group(1)
        notes = []
        for sub_key, sub_value in big_dict.items():
            if key in sub_value:
                notes.append(sub_key[:-4])
        tmp_list.append((tmp, notes))

    cmd_write_list = ["\t\t\"cmd\" : {\n"]
    for index in range(len(tmp_list)):
        tmp_string = re.sub("'", "\"", tmp_list[index][0])
        if "\t\t\t# " + " ".join(tmp_list[index][1]) + "\n" != "\t\t\t# " + " ".join(tmp_list[index-1][1]) + "\n":
            cmd_write_list.append("\t\t\t# " + " ".join(tmp_list[index][1]) + "\n")
        cmd_write_list.append("\t\t\t" + tmp_string + "\n")
    cmd_write_list.append("\t\t},\n")

    with open("res.txt", 'w') as f:
        f.writelines(rest_content[:insert_index])
        f.writelines(cmd_write_list)
        f.writelines(rest_content[insert_index:])


if __name__ == "__main__":
    run(["a2e.txt", "a2r.txt", "q2e.txt"])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
options: -s single-line mode -m multi-line mode -i ignoreCase -G global -R from right to left -E extended-regexp pattern ERE -e ="PAT-regexp" -C ="strToOperate" -F fixed-strings fgrep -c ="num" print only a count of matching first lines per file -n force the prefixing filename on output -N without prefixing filename on output -o show only the part of the line that matched -f ="file" read regex patterns from file -q quiet suppress all normal output,stderr etc -O output file offsets, not text -x print line number with output lines -r recursively scan sub-directories -I select only non-matching lines -S ="replaceto" Match(regexp)-->replaceto -p ="bak-suffix-name",Modify file(S) directly,will create backup file(s) if bak- suffix-name is not null -d ="dir to search" default=currentDirectory,dir-str can't end with \",avoid esc ape " -X mask filename to be operated with regex -T only output filename and match num for per file search file:if set the -e arg and no (-C arg || stdin-pipe || inputfile),then ou tput filepath that match -ergx;if -o arg is set,output filenames only 从命令行中-C参数指定要处理的字符串: C:\>E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -Chello2012 -e2\d+2 Line:1 Offset:5 hello2012 //output -o参数指定只输出匹配部分: C:\>E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -Chello2012 -e2\d+2 -o 2012 从标准输入 管道里获取要处理的数据 C:\>help |E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -e^V.+?\s -o VERT VE VER VER VERIFY VOL 从文件里读取数据处理,支持递归处理目录 E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -ewindows Boo-t.ini -i Boo-t.ini result : Line:3 Offset:45 (0)partition(1)\WINDOWS [operating sys Line:5 Offset:37 (0)partition(1)\WINDOWS="Microsoft Wind Line:5 Offset:56 DOWS="Microsoft Windows XP Professional -d指定目录,-r表示递归处理 -e指定要查找的表达式,如果没有带处理数据(即没有-C\管道输入\指定文件)但指定了目录,则会 处理目录下的目录名和文件名,并输出符合表达式的路径名称 E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -eboot.i*i -i -d"c:" -r c:\boot.ini E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -eboot.i*i -i -d"c:\\"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值