Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
grep管道python等价物_Python_Bash - Fatal编程技术网

grep管道python等价物

grep管道python等价物,python,bash,Python,Bash,我使用这个bash命令捕获文本文件中的字符串 cat a.txt | grep 'a' | grep 'b' | grep 'c' | cut -d" " -f1 如何在python中实现此解决方案?我不想调用os命令,因为它应该是一个跨平台脚本。您可以试试这个 with open(file) as f: # open the file for line in f: # iterate over the lines if all(i in li

我使用这个bash命令捕获文本文件中的字符串

cat a.txt | grep 'a' | grep 'b' | grep 'c' | cut -d" " -f1
如何在python中实现此解决方案?我不想调用os命令,因为它应该是一个跨平台脚本。

您可以试试这个

with open(file) as f:      # open the file
    for line in f:         # iterate over the lines
        if all(i in line for i in ('a', 'b', 'c')): # check if the line contain all (a,b,c)
            print line.split(" ")[0]   # if yes then do splitting on space and print the first value

您始终可以使用os库执行系统调用:

 import os

 bashcmd = " cat a.txt | grep 'a' | grep 'b' | grep 'c' | cut -d' ' -f1"
 print os.system( bashcmd )