因此,我通常在 Bash 脚本中使用的 bash 命令类似于:
$ cat huge2GBfile.txt | grep -w "pattern1/|pattern2/|pattern3" > out.txt
它将在huge2GBfile 中输出找到pattern1、2、3 的行。我想知道这是否可以通过python实现。我知道我可以使用
os.system(cmd)
但我想知道 Python 中是否有类似的东西(我是一个完全的菜鸟),以及它是否比使用 cat+grep 更快。谢谢!
最初的想法,会像
for line in f:
if pattern in line:
out.write(line)
更快?