简体   繁体   English

python 脚本:转换 bash grep 并排序行以将其插入我的 Z23EEEB4347BDD26BFC6B7EE9A37 one

[英]python script : convert a bash grep and sort line to insert it in my python one

Here is what I had on bash & need to convert on python ( mandatory no choice in my company ) but it's cool to learn new things:这是我在 bash 上的内容,并且需要在 python 上进行转换(我公司强制没有选择),但学习新事物很酷:

content of input test file is made of lines looking like输入测试文件的内容由看起来像的行组成

2022-08-11 13:53:15 ; INFO ; file=toto ; Upload size = 13 KB ; result = ....
2022-08-11 13:54:55 ; other info ; rate = 5.3  ; 
2022-08-11 13:57:02 | not to be kept line 
2022-08-11 13:59:15 ; INFO ; file=titi ; Upload size = 3 KB ; result =...

and so on but the real file will contain other log lines formats (for security reasons I cannot copy here a real line) so I use a test file依此类推,但真实文件将包含其他日志行格式(出于安全原因,我无法在此处复制真实行)所以我使用测试文件

here is the exact command that give the attended output这是给出参加的 output 的确切命令

grep -ihE "size|rate|type_[DI][TA][FT]|source|dest" ../data/*.{log,debug} | sort -t " " -k1,6 -k2 > filtre.txt

So first I want to try it without creating the outputfile所以首先我想在不创建输出文件的情况下尝试它

here is what I am trying to with python (I'm limited to 2.7 & cannot choose anything else do not ask or mention about it)这就是我想用 python 做的事情(我仅限于 2.7 并且不能选择其他任何东西,不要询问或提及它)

import os
import re
import string
import sys 

datalogpath = sys.argv[1]       #  get the path of log files to extract datas

searchpattern = re.compile("size|rate|type_D|type_I|source|dest")  # regexp to filter from logs directory

# step 1- equ grep all from 


for filename in os.listdir(datalogpath):
    with open(os.path.join(datalogpath, filename)) as in_file:
        for line in in_file:
            found = searchpattern.search(line)
            if found :
                print(found.group(0))

What's currently seem to appear from test file is only目前似乎从测试文件中出现的只是

size
size
size
size
size

instead of the each full lines containing size or any of the other words I'm looking for the grep command replies with the all 23 lines (all content of each)而不是包含大小或任何其他单词的每一整行,我正在寻找 grep 命令回复所有 23 行(每行的所有内容)

like喜欢

2022-08-11 13:53:15 ; INFO ; file=toto ; Upload size = 13 KB ; result = ....
2022-08-11 13:54:55 ; other info ; rate = 5.3  ; 
2022-08-11 13:59:15 ; INFO ; file=titi ; Upload size = 3 KB ; result =...

so for example line所以例如线

2022-08-11 13:57:02 | not to be kept line 

is not displayed on output在 output 上不显示

none of the official documentation chapters was fitting this use-case,没有任何官方文档章节适合这个用例,

Please help me redefine the correct regexp in python format and/or file reading method if this one is bad请帮我重新定义 python 格式的正确正则表达式和/或文件读取方法,如果这个是错误的

change the改变

                print(found.group(0))

to

                print(line)

you want to display the full line when there is a match, not just what the re matched against.您希望在匹配时显示整行,而不仅仅是重新匹配的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM