Grep for a word, and if found print 10 lines before and 10 lines after the pattern match

后端 未结 5 606
温柔的废话
温柔的废话 2021-01-01 05:32

I am processing a huge file. I want to search for a word in the line and when found I should print 10 lines before and 10 lines after the pattern match. How can I do it in P

5条回答
  •  [愿得一人]
    2021-01-01 06:31

    Try this

    #!/usr/bin/python
    import commands
    
    filename = "any filename"
    string_to_search = "What you want to search"
    
    extract  = (commands.getstatusoutput("grep -C 10 '%s' %s"%(string_to_search,filename)))[1]
    
    print(extract)
    

提交回复
热议问题