1

I read the related Questions before asking my question but it's kind of problem specific. I am executing a bash script and the result is stored in a .txt file. Immediately after the file is created, I need to use a number which is contained in the file to do some operations in Python. I take the variable of interest using grep as follows.

MeasureImageSimilarity.sh 3 2 myfile1 myfile2 results.txt
var=$(grep -Ewo "[+-]?[0-9]" result.txt)

My question now is how can i create an array of these "var" variables in Python if i put the above script in a loop. I am asking because i also need the indexes of the hypothetical for-loop that i want to iterate in.

3
  • Please add a sample text file which you're working with Commented Feb 27, 2015 at 17:23
  • mywiki.wooledge.org/BashGuide/Arrays ? Commented Feb 27, 2015 at 17:25
  • just run the grep using subprocess or use re Commented Feb 27, 2015 at 17:33

1 Answer 1

1

You could do something like this:

a=subprocess.check_output('(grep -Ewo "[+-]?[0-9]" result.txt)', shell=True)

Since this will return a string you can perform:

a=float(a)

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.