In an unix script the following piece of code is present:
grep -E 'value1' file1.txt | grep 'value2' | grep 'value3' | grep 'value3'
The above command is grepping for all those variables from file.txt and based on the result writing a 'line' in file1 else will write the 'line' in file2
I want to replicate the same functionality in Python.
I created an array with the values for the variables:
regexarr = ['value1', 'value2', 'value3', 'value4']
Then I opened the file as:
with open('file1.txt', 'r') as file1:
# then I have the below code to match the strings in the regexarr
if any(re.findall('|'.join(regexarr), file1.read())):
with open ('file2.txt', 'a+') as file2:
file2.write(eachline)
else:
with open('file3.txt', 'a+') as file3:
file3.write(eachline)
with the above code, nothing is written to file3.txt even though I have test data which I want to get written to file3.txt
How can I get the same functionality as in unix in python?
value1infile1.txt, then in only in the result looks forvalue2, and so forth. You end up with lines containing all of the values in any order, possibly overlapping. Is this what you want?value4?