简体   繁体   English

逐行搜索正则表达式,并在匹配后打印多行

[英]search regex line by line and print multiple lines after match

just started writing scripts. 刚开始编写脚本。

I am trying to write 4 lines after matching a regex in the first line and do this through a very large file with thousands of lines. 我试图在第一行匹配一个正则表达式后写4行,并通过一个包含数千行的非常大的文件来完成。

#!/usr/local/bin/python
import sys
import string
import re

print"what file would you like to search?"
filename = sys.stdin.readline()
filename = filename.rstrip()
print "enter a barcode"
barcode=sys.stdin.readline()
barcode=barcode.rstrip()
regex=":0:"+barcode
infile = open(filename, "r")
outfile = open("sample_write.fastq", "w")
regex_object = re.compile(regex, re.I)
for line in infile:
   m=regex_object.search(line)
   if m:
      outfile.write(line)
exit

This writes the lines that have matched string (barcode) into my outfile, however, I need to write both the line with the barcode and the following 3 lines after the match into my target file. 这会将与字符串(条形码)匹配的行写入我的输出文件中,但是,我需要将具有条形码的行以及匹配后的以下3行写入我的目标文件中。 Any idea ? 任何想法 ?

You can use the following to match 您可以使用以下内容进行匹配

.*(BARCODE).*\n.*\n.*\n.*    //replace BARCODE with your variable

and write the matches ( \\0 ) to your target file 并将匹配项( \\0 )写入目标文件

See DEMO 演示

Edit: Use regex =".*("+barcode+").*\\n.*\\n.*\\n.*" 编辑:使用regex =".*("+barcode+").*\\n.*\\n.*\\n.*"

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

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