I've tried to read a txt file and find the lines which contain a certain word "checkout_revision". I want to find these lines one by one in a for loop and store them in my variable, let say temp. I heard grep
with cut
is suitable for this. However I could not do that. Is there anyone to help me? Here is my code :
for line in intersect:
cmd=""" grep "CHECKOUT_REVISION" |cut -d\'\"\' -f2"""%fst_directory
cmd_test=os.system(cmd)
Best Answer
Suppose there is a file
/home/eday/test.txt
with the contents bellow:The following Python script will read the file stored in
my_file
variable looking for what is stored inlook_for
variable and if it finds a match, it will store it intemp
variable which is a list variable.Finally it will print to the output the contents of
temp
You can comment out or delete the printing line.the above script will have the following output in terminal: