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.
-
Please add a sample text file which you're working withForceBru– ForceBru2015年02月27日 17:23:52 +00:00Commented Feb 27, 2015 at 17:23
-
mywiki.wooledge.org/BashGuide/Arrays ?Etan Reisner– Etan Reisner2015年02月27日 17:25:20 +00:00Commented Feb 27, 2015 at 17:25
-
just run the grep using subprocess or use rePadraic Cunningham– Padraic Cunningham2015年02月27日 17:33:33 +00:00Commented Feb 27, 2015 at 17:33
1 Answer 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!