Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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)
share|improve this question
1  
Why do this in bash? You'd rather open the file in python (or whatever language you are using) and do this operation there. –  panmari Aug 23 '13 at 15:35
    
Yea , Why I couldnt have been think that lol –  caesar Aug 23 '13 at 15:37

3 Answers 3

Suppose there is a file /home/eday/test.txt with the contents bellow:

this is a test


another line

CHECKOUT_REVISION this must be stored

some other things
CHECKOUT_REVISION another line to store

The following Python script will read the file stored in my_file variable looking for what is stored in look_for variable and if it finds a match, it will store it in temp 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.

#!/usr/bin/env python

# path to the file to read from
my_file = "/home/eday/test.txt"
# what to look in each line
look_for = "CHECKOUT_REVISION"
# variable to store lines containing CHECKOUT_REVISION
temp = []

with open(my_file, "r") as file_to_read:
    for line in file_to_read:
        if look_for in line:
            temp.append(line)

# print the contents of temp variable
print (temp)

the above script will have the following output in terminal:

$ ['CHECKOUT_REVISION this must be stored', 'CHECKOUT_REVISION another line to store']
share|improve this answer
result = []
for line in open('filename'):
    if 'CHECKOUT_REVISION' in line:
        result.append(line.split('\'"\'')[1])

I suppose this is what you want - you get a list of strings with second field of each line which contain string CHECKOUT_REVISION. The question should be moved to stackoverflow though.

share|improve this answer
    
I want to search on a file . –  caesar Aug 23 '13 at 16:51
    
@Eday well okay, if intersect is not a variable holding strings, then you go with for line in open('filename'):. It is quite simple and there's really no need for external tools, if you only check if a substring is present and then split line into records. –  moon.musick Aug 23 '13 at 17:17

I agree that Stef's answer is a good answer, but if you still want to use grep or cut (or others) commands from bash in python, I suggest you do not use os.system, but to use subprocess module. For example:

#!/usr/bin/env python

import subprocess

cmd = "grep CHECKOUT_REVISION /home/eday/test.txt | cut -d'\"' -f2 -s"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
temp = process.communicate()[0]

print (temp)

If /home/eday/test.txt file is something like this:

some lines
CHECKOUT_REVISION="revision one"
some other lines
CHECKOUT_REVISION="revision two"
other lines

the output of the above python script will be:

revision one
revision two
share|improve this answer
    
@radu-rdeanu why dont use os.system / –  caesar Aug 24 '13 at 12:06
    
@Eday Using subprocess module you can control the stdout. os.system is deprecated. See stackoverflow.com/q/5596911/2353900 –  Radu Rădeanu Aug 24 '13 at 12:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.