PDA

View Full Version : How to separate two numbers from the same line.Little help here please..!!!



nikkpap
November 9th, 2010, 02:48 AM
hi i want to ask if we can separate into x= and y= from the a file with the following text <coordinates>39,67882574656104,59,50864780739133,0</coordinates>
i use
cpath=$ "~/Desktop" #just a test path.
value=$(grep '\<coordinates\>'$cpath *.kml) # with this i take the coordinates
notify-send "$value" #just to test the value faster than echo ;)

i just want to use
x=39,67882574656104
y=59,50864780739133 and continue with further calculations.
thanks in advance.
nikkpap@gmail.com

worksofcraft
November 9th, 2010, 03:03 AM
hi i want to ask if we can separate into x= and y= from the a file with the following text <coordinates>39,67882574656104,59,50864780739133,0</coordinates>
i use
cpath=$ "~/Desktop" #just a test path.
value=$(grep '\<coordinates\>'$cpath *.kml) # with this i take the coordinates
notify-send "$value" #just to test the value faster than echo ;)

i just want to use
x=39,67882574656104
y=59,50864780739133 and continue with further calculations.
thanks in advance.
nikkpap@gmail.com

my first reaction is that you do well to use a decimal point instead of a decimal comma because using the same caharceter to separate between the two numbers is really gunna cause problems in the long run.

cipherboy_loc
November 9th, 2010, 03:33 AM
Where [file] is the file where the thing is stored:


grep -io '<coordinates>[0-9, \.]\{1,\}</coordinates>' ./[file] | grep -oi '[0-9,]\{1,\}' | grep -io '[0-9]\{2\},[0-9]\{1,\}'

I could not get the last number (,0), but other than that..


Cipherboy

SeijiSensei
November 9th, 2010, 06:10 AM
You can use sed and awk for this, though the fact that you use the comma as both a decimal point and a field separator makes it a bit more annoying than if you used a period.



value=$(grep '\<coordinates\>'$cpath *.kml | sed 's/<.coordinates>//g')
x=$(echo $value | awk -F, '{print $1 "," $2}')
y=$(echo $value | awk -F, '{print $3 "," $4}')


The first line uses sed to remove the <coordinates> and </coordinates> elements, then awk splits the result using the comma as a delimiter. Each field is referenced as $n and a literal comma is inserted between the fields using "," in awk's print command.

sreek_arc
November 9th, 2010, 08:22 AM
See my not so elegant sed solution

sed s'/<[a-z]*>//g' inputfilename | sed s'/<\/[a-z]*>//g' | sed s'/,/\t/2

sed one lines are great

nikkpap
November 10th, 2010, 02:12 PM
i used a combination of your propositions and toke this

cdts=$( grep '\<coordinates\>' $cpath | sed 's/<.coordinates>//g'| grep -oi '[0-9,]\{1,\}' )
xfa=$(echo $cdts | awk -F, '{print $1 "," $2}')
yfa=$(echo $cdts | awk -F, '{print $3 "," $4}')
notify-send "$xfa"
notify-send "$yfa"

thanks you all when i finish the script i will sent it to see it... ;)

P.S. where i can find how to use mathematics calculations in bash
ex.
r = (x2 + y2)^1/2
θ = atan(y/x)
x = r cos(θ)
y = r sin(θ)

SeijiSensei
November 10th, 2010, 02:17 PM
P.S. where i can find how to use mathematics calculations in bash
ex.
r = (x2 + y2)^1/2
θ = atan(y/x)
x = r cos(θ)
y = r sin(θ)

You can only do basic arithmetic operations in the shell. See http://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic. No trig functions or anything like that. To do advanced calculations within an interpreted language, you'll need to use something like Python.

trent.josephsen
November 10th, 2010, 02:54 PM
You can use bc from a shell to do mathematical operations fairly easily:

$ bc -l <<<'sqrt(0.3^2+0.4^2)'
.50000000000000000000
$ bc -l <<<'a(1)*4'
3.14159265358979323844
$ bc -l <<<'c(0)'
1.00000000000000000000

It's not available everywhere, though.

nikkpap
November 10th, 2010, 11:32 PM
May i continue in this direction http://www.suite101.com/content/mathmatical-functions-and-linux-shells-a66284

with korn shell is it possible to make my calculations with this kind of shell i think all Ubuntu distros which i care more they have pre-installed the korn shell

and thank you all guys you are awesome ... really this is the way i want to live my life i like to share my knowledge open source open mind for ever for a better word without piracy .... thanks again