0

我正在使用 python3 并尝试从输入文件中动态提取第 2 个参数并将其转储到单独的文件中。strings_to_filter :- 我应该在这里传递静态数据..我有点怀疑..

我们能否从输入文件中获取此值...而不是将其传递给此 strings_to_filter 作为

“CAPTIVE_EXECUTE_CMD”、“CAPTIVE_EXECUTE_CMD_FAILED”、“CAPTIVE_RECVD_SIGCHLD”、“LOG_LEVEL_DEBUG”、“LOG_LEVEL_DEBUG_ERR”、“LOG_LEVEL_DEBUG”、

#!/usr/bin/env python3

import os
import re

def extract_lines(file_in, file_out, strings_to_filter):
    with open(file_in) as f_in:
        for line in f_in:
            res = re.search("(PUT_LOG\()(.*)(\)\;)", line)
            if res is not None:
                i = 0
                for segment in res.group(2).split(","):
                    segment = segment.strip()
                    if segment in strings_to_filter and i < 2:
                        with open(file_out, "a+") as f_out:
                            print(segment, file=f_out)
                            i += 1

extract_lines( os.path.realpath("data.c"),
               os.path.realpath("outfile.txt"),
               ["CAPTIVE_EXECUTE_CMD","CAPTIVE_EXECUTE_CMD_FAILED",
                "CAPTIVE_RECVD_SIGCHLD","LOG_LEVEL_DEBUG",
                "LOG_LEVEL_DEBUG_ERR","LOG_LEVEL_DEBUG"]
               )

Please see my data.c file which contains the following line :

From where i want to extract first 2 parameter from PUT_LOG

PUT_LOG( LOG_LEVEL_DEBUG, CAPTIVE_RECVD_SIGCHLD, 3, INT, pid,INT, status, INT, errno);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_EXECUTE_CMD,1,STRING, cmd);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_EXECUTE_CMD_FAILED,1, STRING, cmd);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_NO_RAD_SERVERS, 3, IP_INT,(g_radius_cfg.cfg[rad_idx].primary_auth.ip),IP_INT, (g_radius_cfg.cfg[rad_idx].secondary_auth.ip),INT,rad_idx);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_GET_RAD_GRP_FAILED,0);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_GET_RAD_GRP_FAILED, 0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_LOGIN_COUNT,1,INT, guest_cfg->login_usrs_count);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_USER_MAC_COUNT, 2,STRING, guest_cfg->login_cfg[i].user,INT, usr_mac_info->mac_count);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_SEND_USER_LIST, 1, INT, resp->count);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_ADD_ACCOUNT, 4, INT,guest_cfg->cap_accnt_count,STRING,req->account_name, INT, req->expiry_num, INT, req->expiry_type );
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_DEL_ACNT, 1, STRING, req->account_name);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_ACCNT_EDIT, 3, STRING,account_cfg->account_name, INT, account_cfg->expiry_num,INT, account_cfg->expiry_type);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_ADD_RAD_USER, 1, STRING,user);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_USR_ALREADY_CREATED, 1,STRING, usr_mac_info->usr_bin);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_CALCULATE_EXPIRY_FAILED,1, STRING, login_cfg->user);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_EXPIRY_SCHEDULE_FAILED,1, STRING, login_cfg->user);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_USER_ALREADY_EXPIRED,1, STRING, login_cfg->user);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_CALCULATE_EXPIRY_FAILED,1, STRING, login_cfg->user);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_EXPIRY_TIME_ERR, 0);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_MAX_CLIENTS_REACHED,1, STRING, guest_auth_info.user);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_MALLOC_FAILED,0);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_MALLOC_FAILED, 0);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_AUTH_TO_APMGR_FAILED, 0);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_ADD_GUEST, 1,STRING, req->email);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_USR_GUEST_IDX, 2,STRING, email_info->email, INT, guest_idx);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_MALLOC_FAILED,0);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_ADD_USER, 1, STRING, req->user);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_USR_GUEST_IDX, 2,STRING, req->user, INT, guest_idx);
PUT_LOG(LOG_LEVEL_INFO,CAPTIVE_ADD_USER_ACCOUNT,2, STRING, req->user, STRING, req->account_name);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_CALCULATE_EXPIRY,4, STRING,req->user, STRING, account_cfg->account_name, INT,account_cfg->expiry_num, INT, account_cfg->expiry_type);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_USER_EXPIRED, 1, STRING,user);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_USER_DONT_EXIST,STRING, user);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_USER_ALREADY_EXPIRED,1, STRING, cfg->user);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_USER_EXPIRY_SCHEDULE, 4,STRING, cfg->user, STRING, ctime(&time_now),STRING, ctime(&schedule_time), INT, delay);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_DELETE_USER, 1,STRING, req->user)
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_EDIT_USER, 2,STRING, req->user, STRING, req->passwd);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_CALCULATE_EXPIRY,4, STRING,req->user, STRING, account_cfg->account_name, INT,account_cfg->expiry_num, INT, account_cfg->expiry_type);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_CFG_CHANGE_NOT_FAILED,0);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_REAUTH_AND_MAX_CL_CFG,3, INT,cfg->inact_hr,INT, cfg->inact_min, INT,cfg->max_clients);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_UPLOAD_FAILED, 0 );
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_FILE_COPY_FAILED, 0);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_ERR_COPY_F_TO_SERV, 0);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_EULA_COPY_FAILED, 0);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_EULA_OPEN_FAILED, 0);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_EULA_TO_CONF_ERR, 0);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_URL_REDIR_STRING,1, STRING,g_url_redirect_str);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_SEND_NOTIF_APMGR,2, INT, g_cfg.guest_cfg.user_type, INT,g_cfg.guest_cfg.reauth_timeout);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_NULL_MAC_ERR, 0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_ADD_MAC_TO_USR, 2,MAC_ADDR, mac, INT, usr_mac_info->mac_count);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_MAC_COUNT, 1, INT, usr_mac_info->mac_count);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_USER_AUTH, 2,STRING, req->user, MAC_ADDR, req->mac_info[0].mac_bin);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_USR_NOT_FOUND, 1,STRING, req->user);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_ACCNT_NAME, 1, STRING,login_cfg->account_name);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_MAX_CLIENTS_REACHED, 1,STRING, req->user);
PUT_LOG(LOG_LEVEL_INFO, CAPTIVE_CLEAR_ALL_GUEST,0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_INDEX_USED, 2,INT, i, INT, usr_info_tbl[i].flag);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_RAD_REDUND_AVOID,0);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_UNKNWN_MSG, 1, INT,hdr.msg_id);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_REPLYING_NOW,0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_UPD_USR_ASSOC_INFO, 3, STRING,guest_assoc_info->user, MAC_ADDR, guest_assoc_info->mac_bin,INT, guest_assoc_info->evt);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_USER_DONT_EXIST, 1,STRING, guest_assoc_info->user);
PUT_LOG(LOG_LEVEL_DEBUG , CAPTIVE_SYSTEM_READY,0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_RAD_CFG_EDIT, 0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_REDUND_DISABLE,0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_REDUND_STATE, 1,INT, red_state_info->state);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_UPD_ASSOC_FAILED,0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_LOG_LEVEL_CHANGE, 1, INT, *captive_log_level);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_CREATE_EULA_FILE, 0);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_EULA_COPY_FAILED, 0);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_EULA_OPEN_FAILED, 0);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_URL_REDIR_STRING,1, STRING,g_url_redirect_str);
PUT_LOG(LOG_LEVEL_DEBUG, CAPTIVE_INIT,0);
PUT_LOG(LOG_LEVEL_ERR, CAPTIVE_CONFIG_INIT_FAILED,0);
PUT_LOG(LOG_LEVEL_DEBUG_ERR, CAPTIVE_IPC_INIT_FAIL,INT, errno);
4

0 回答 0