Extracting From A File Then Processing

Hi. Im working with this data in a file:

-94.49109387652327,39.2956736296775
-93.0906917141962,38.72762798197614
-90.57659976220785,-40.25685140137304
-92.340961875134,39.44522321129584
92.340961875134,39.44522321129584
-94.72083812873272,37.63567097374739 

Is there a way to set everything before the comma as say var1 and everything after the comma as var2. Then be able to process those vars, then move onto the next set until theres no more sets?

All that data was extracted from a KML using:

sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml  

The line it extracts from looks like this:

<coordinates>-90.57659976220785,40.25685140137304,0</coordinates>

Maybe do the same thing only extracting them individually from the KML instead of the other file and then processing them, then moving to the next and so forth?

nawk -F'[>,]' '{print $2, $3}' Test.kml

Hi.

You could try something like:

sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml | while IFS=, read A B; do
  echo a is $A   b is $B
  ...
done

(this assumes that your Test.kml looks like the data you posted after sed processed it)

i.e.

-94.49109387652327,39.2956736296775
-93.0906917141962,38.72762798197614
-90.57659976220785,-40.25685140137304
-92.340961875134,39.44522321129584
92.340961875134,39.44522321129584
-94.72083812873272,37.63567097374739

Then the output is

a is -94.49109387652327 b is 39.2956736296775
a is -93.0906917141962 b is 38.72762798197614
a is -90.57659976220785 b is -40.25685140137304
a is -92.340961875134 b is 39.44522321129584
a is 92.340961875134 b is 39.44522321129584
a is -94.72083812873272 b is 37.63567097374739

This is just to compile some stuff for my reference.

sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml 
-94.49109387652327,39.2956736296775
-93.0906917141962,38.72762798197614
-90.57659976220785,40.25685140137304
-92.340961875134,39.44522321129584
-92.340961875134,39.44522321129584
-94.72083812873272,37.63567097374739


sed -n 's/.*<longitude>\(.*\)<\/longitude>.*/\1/ip;T' Test.kml 
-94.49109387652327
-93.0906917141962
-90.57659976220786
-92.340961875134
-92.340961875134
-94.72083812873272

#Math with decimals
a=`echo "1+1.2" | bc` && echo $a
2.2

a=`dc -e '1.2 1+p'`
echo $a
2.2

#Extracting coords as vars then processing
sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml | while IFS=, read A B; do
  echo a is $A   b is $B
  echo "asdf"
done
a is -94.49109387652327 b is 39.2956736296775
a is -93.0906917141962 b is 38.72762798197614
a is -90.57659976220785 b is 40.25685140137304


#Multi Condition if statement
if [ $i = "+" -o $i = "-" -o $i = "/" -o $i = "%" ]; then
$x=$vr1
else
print "You have entered an invalid option."

I never thought of the site as a personal organiser before :wink: Have a word with Neo. He might want to endorse it!

Hah yea it just made sense. Easiest way to transport code from home to work. I tried out

sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml | while IFS=, read A B; do
  echo a is $A   b is $B
  echo "asdf"
done

at home on Ubuntu 9.04 and it worked but now it says that the command is garbled on solaris 10. I had a feeling this would happen as Im sure my Solaris box must be running an older version of pretty much everything. Can anyone think of a more flexible or older sed/bash friendly way perhaps?

Hi.

Well, the nawk from vger works fine, which would leave you with:

nawk -F'[>,]' '{print $2, $3}' | while read A B; do
  echo a is $A   b is $B
done

(use /usr/xpg4/bin/awk if you don't have nawk)

Or an alternative sed:

sed 's/.*<coordinates>\(.*\)<.*/\1/'  | while IFS=, read A B junk; do
  echo a is $A   b is $B
done

When I run

nawk -F'[>,]' '{print $2, $3}' a.txt | while read A B; do
  echo a is $A   b is $B
done

on a.txt

-94.49109387652327,39.2956736296775
-93.0906917141962,38.72762798197614
-90.57659976220785,40.25685140137304
-92.340961875134,39.44522321129584
-92.340961875134,39.44522321129584
-94.72083812873272,37.63567097374739

I get

a is 39.2956736296775 b is
a is 38.72762798197614 b is
a is 40.25685140137304 b is
a is 39.44522321129584 b is
a is 39.44522321129584 b is
a is 37.63567097374739 b is

Oh and I got the raw coords for a.txt from the Test.kml using

sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml

But the sed command does work fine on the a.txt at home hopefully its true for work too!

Well,

The awk is supposed to run against this:

<coordinates>-90.57659976220785,40.25685140137304,0</coordinates>

(i.e. Test.kml)

Not against this:

-90.57659976220785,40.25685140137304

(i.e. a.txt)

My understanding is that you would run "awk .. | while read" against the kml file.