简体   繁体   English

Python:从文件列表中的file1搜索模式

[英]Python : Search patterns from file1 in a list of files

I have a file which contains all the list of patterns. 我有一个包含所有模式列表的文件。 The patterns are of this format (high:low). 模式具有这种格式(高:低)。 high and low separated by a colon. 高和低之间用冒号隔开。

Sample patterns in file1: file1中的样本模式:

-6447867851221037056:-3788579599719006014 -6447867851221037056:-3788579599719006014

-6449238495544274944:-3932696454242172736 -6449238495544274944:-3932696454242172736

-6449265231715692544:-4004752252983770939 -6449265231715692544:-4004752252983770939

-6449203349826891776:-2995947018784538426 -6449203349826891776:-2995947018784538426

-6447968581089030144:-3659104829784325951 -6447968581089030144:-3659104829784325951

-6449244980944891904:-1059398397250633536 -6449244980944891904:-1059398397250633536

-6449247532155465728:-1915082300761767744 -6449247532155465728:-1915082300761767744

-6447984223359922176:-4220924871888797497 -6447984223359922176:-4220924871888797497

Now, I have a list of files in a search directory. 现在,我在搜索目录中有了文件列表。 I want to search the patterns from the above file in all the files in the search directory which matches file * yyyy-mm-dd-hh * pattern. 我想在与文件* yyyy-mm-dd-hh *模式匹配的搜索目录中的所有文件中,从上述文件中搜索模式。

eg: 例如:

Search all the patterns from file1 in search directory with filename pattern * 2015-09-07-06 * 使用文件名模式从搜索目录中的file1搜索所有模式* 2015-09-07-06 *

search_file.2015-09-07-05-45 search_file.2015-09-07-05-45

search_file.2015-09-07-06-47 search_file.2015-09-07-06-47

search_file.2015-09-07-06-48 search_file.2015-09-07-06-48

search_file.2015-09-07-06-50 search_file.2015-09-07-06-50

search_file.2015-09-07-06-52 search_file.2015-09-07-06-52

I know how to do this in bash (grep -f file1 * 2015-09-07-06 * ). 我知道如何在bash中执行此操作(grep -f file1 * 2015-09-07-06 * )。 But I'm new to python and have very little clue of how to proceed further. 但是我是python的新手,对如何进一步进行了解甚少。 Any kind of pointers is appreciated 任何类型的指针都值得赞赏

Checkout this package: https://amoffat.github.io/sh/ 签出这个包: https : //amoffat.github.io/sh/

It will enable you to run your shell command in python easily. 这将使您能够轻松地在python中运行shell命令。

You can use pip to install it. 您可以使用pip进行安装。

The efficient and easy way is with shell commands. 高效而简便的方法是使用shell命令。 Hope you will be knowing os.system for execute shell commands in python. 希望您知道os.system在python中执行shell命令。 I have tried the same logic in python. 我在python中尝试了相同的逻辑。

    import glob
    file_list = glob.glob("*2015-09-07-06*")

    input_filename = "file1.txt"
    with open(input_filename,'r') as input:
        input_data = input.readlines()

    for each_file in file_list:
        open_file = open(each_file,'r')
        file_data = open_file.readlines()
        identified = set(input_data) & set(file_data)
        if identified:
            print "filename : " + str(each_file)+" : "+str(identified)
import re
def searchpattern():
    with open("file1.txt", 'r') as fobj:
        f = fobj.readlines()
        pattern = '2015-09-07-06'

        for i in f:
            m = re.search(pattern, i)
            if m:
                print(i)
            else:
                pass
if __name__ == '__main__':
    searchpattern()

Output: 输出:

2015-09-07-06-45
2015-09-07-06-46

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

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