Extract coordinates from KML using shell tools

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Test.kmz</name>
<Placemark>
<name>Test</name>
<LookAt>
<longitude>48.7893522149238</longitude>
<latitude>35.11072013511394</latitude>
<altitude>0</altitude>
<heading>-0.1257146222858709</heading>
<tilt>27.02159138599089</tilt>
<range>804.546845381412</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#m_ylw-pushpin160</styleUrl>
<Point>
<gx:drawOrder>1</gx:drawOrder>
<coordinates>48.78741496263481,35.11017636516465,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>


I would like to extract the 48.78741496263481,35.11017636516465 from a KML (but in reverse order) as 35.11017636516465,48.78741496263481. Is this a task for sed or awk?



Thanks in advance.







share|improve this question



















  • Consider using an XML parser for this e.g. xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -c '//kml:coordinates/text()' -n yourfile.xml or xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -v 'concat(//kml:latitude,",",//kml:longitude)' -n yourfile.xml . See for example Why does this XPath expression not return the correct value in xmlstarlet?
    – steeldriver
    Jul 30 at 12:39
















up vote
0
down vote

favorite












<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Test.kmz</name>
<Placemark>
<name>Test</name>
<LookAt>
<longitude>48.7893522149238</longitude>
<latitude>35.11072013511394</latitude>
<altitude>0</altitude>
<heading>-0.1257146222858709</heading>
<tilt>27.02159138599089</tilt>
<range>804.546845381412</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#m_ylw-pushpin160</styleUrl>
<Point>
<gx:drawOrder>1</gx:drawOrder>
<coordinates>48.78741496263481,35.11017636516465,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>


I would like to extract the 48.78741496263481,35.11017636516465 from a KML (but in reverse order) as 35.11017636516465,48.78741496263481. Is this a task for sed or awk?



Thanks in advance.







share|improve this question



















  • Consider using an XML parser for this e.g. xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -c '//kml:coordinates/text()' -n yourfile.xml or xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -v 'concat(//kml:latitude,",",//kml:longitude)' -n yourfile.xml . See for example Why does this XPath expression not return the correct value in xmlstarlet?
    – steeldriver
    Jul 30 at 12:39












up vote
0
down vote

favorite









up vote
0
down vote

favorite











<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Test.kmz</name>
<Placemark>
<name>Test</name>
<LookAt>
<longitude>48.7893522149238</longitude>
<latitude>35.11072013511394</latitude>
<altitude>0</altitude>
<heading>-0.1257146222858709</heading>
<tilt>27.02159138599089</tilt>
<range>804.546845381412</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#m_ylw-pushpin160</styleUrl>
<Point>
<gx:drawOrder>1</gx:drawOrder>
<coordinates>48.78741496263481,35.11017636516465,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>


I would like to extract the 48.78741496263481,35.11017636516465 from a KML (but in reverse order) as 35.11017636516465,48.78741496263481. Is this a task for sed or awk?



Thanks in advance.







share|improve this question











<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Test.kmz</name>
<Placemark>
<name>Test</name>
<LookAt>
<longitude>48.7893522149238</longitude>
<latitude>35.11072013511394</latitude>
<altitude>0</altitude>
<heading>-0.1257146222858709</heading>
<tilt>27.02159138599089</tilt>
<range>804.546845381412</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#m_ylw-pushpin160</styleUrl>
<Point>
<gx:drawOrder>1</gx:drawOrder>
<coordinates>48.78741496263481,35.11017636516465,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>


I would like to extract the 48.78741496263481,35.11017636516465 from a KML (but in reverse order) as 35.11017636516465,48.78741496263481. Is this a task for sed or awk?



Thanks in advance.









share|improve this question










share|improve this question




share|improve this question









asked Jul 30 at 8:08









Luis Paganini

132




132











  • Consider using an XML parser for this e.g. xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -c '//kml:coordinates/text()' -n yourfile.xml or xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -v 'concat(//kml:latitude,",",//kml:longitude)' -n yourfile.xml . See for example Why does this XPath expression not return the correct value in xmlstarlet?
    – steeldriver
    Jul 30 at 12:39
















  • Consider using an XML parser for this e.g. xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -c '//kml:coordinates/text()' -n yourfile.xml or xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -v 'concat(//kml:latitude,",",//kml:longitude)' -n yourfile.xml . See for example Why does this XPath expression not return the correct value in xmlstarlet?
    – steeldriver
    Jul 30 at 12:39















Consider using an XML parser for this e.g. xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -c '//kml:coordinates/text()' -n yourfile.xml or xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -v 'concat(//kml:latitude,",",//kml:longitude)' -n yourfile.xml . See for example Why does this XPath expression not return the correct value in xmlstarlet?
– steeldriver
Jul 30 at 12:39




Consider using an XML parser for this e.g. xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -c '//kml:coordinates/text()' -n yourfile.xml or xmlstarlet sel -N kml='http://www.opengis.net/kml/2.2' -t -v 'concat(//kml:latitude,",",//kml:longitude)' -n yourfile.xml . See for example Why does this XPath expression not return the correct value in xmlstarlet?
– steeldriver
Jul 30 at 12:39










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Using AWK:



awk -F '[>,]' '/coordinates/ print $3","$2' file


  • search for the pattern coordinates and prints the 3rd field and 2nd field.





share|improve this answer






























    up vote
    0
    down vote













    awk -F"[<>]" '/longitude/long=$3/latitude/print $3","long' file.txt





    share|improve this answer





















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459279%2fextract-point-coordinates-from-kml-using-shell-tools%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      Using AWK:



      awk -F '[>,]' '/coordinates/ print $3","$2' file


      • search for the pattern coordinates and prints the 3rd field and 2nd field.





      share|improve this answer



























        up vote
        0
        down vote



        accepted










        Using AWK:



        awk -F '[>,]' '/coordinates/ print $3","$2' file


        • search for the pattern coordinates and prints the 3rd field and 2nd field.





        share|improve this answer

























          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Using AWK:



          awk -F '[>,]' '/coordinates/ print $3","$2' file


          • search for the pattern coordinates and prints the 3rd field and 2nd field.





          share|improve this answer















          Using AWK:



          awk -F '[>,]' '/coordinates/ print $3","$2' file


          • search for the pattern coordinates and prints the 3rd field and 2nd field.






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jul 30 at 8:42


























          answered Jul 30 at 8:37









          SivaPrasath

          3,46311535




          3,46311535






















              up vote
              0
              down vote













              awk -F"[<>]" '/longitude/long=$3/latitude/print $3","long' file.txt





              share|improve this answer

























                up vote
                0
                down vote













                awk -F"[<>]" '/longitude/long=$3/latitude/print $3","long' file.txt





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  awk -F"[<>]" '/longitude/long=$3/latitude/print $3","long' file.txt





                  share|improve this answer













                  awk -F"[<>]" '/longitude/long=$3/latitude/print $3","long' file.txt






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jul 30 at 8:22









                  Kamaraj

                  2,5341312




                  2,5341312






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459279%2fextract-point-coordinates-from-kml-using-shell-tools%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)