grep piping python equivalent

JayGatsby

I use this bash command for catching a string in a text file

cat a.txt | grep 'a' | grep 'b' | grep 'c' | cut -d" " -f1

How can I implement this solution in python? I don't want to call os commands because it should be a cross platform script.

Avinash Raj

You may try this,

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related