0

I have a script that takes input from the user (Hospital Name, Hospital ID, FAA ID, LAT/LON, City, State. All this information is manipulated various ways to be written to multiple files in different formats. Then, each file is sorted to have the new information put in alphabetical order. I have finished the script, everything works; however, I did not write the code to sort one particular file because its formatting is multiple lines of code within code. Below is the excerpt of my script and an excerpt of the file where this information is being sent.

if [ -z "$FAA" ]; then
sed -i '' "${line} i\\
    <Placemark>\\
      <name>${HOSP}</name>\\
      <description>${CITY}, ${STATE} ${ID}</description>\\
      <styleUrl>#style1</styleUrl>\\
      <Point>\\
        <coordinates>${LON},${LAT},0</coordinates>\\
      </Point>\\
    </Placemark>\\
" "$dir"/Hospital.kml
else
sed -i '' "${line} i\\
    <Placemark>\\
      <name>${HOSP}</name>\\
      <description>${CITY}, ${STATE} ${ID} ${FAA}</description>\\
      <styleUrl>#style1</styleUrl>\\
      <Point>\\
        <coordinates>${LON},${LAT},0</coordinates>\\
      </Point>\\
    </Placemark>\\
" "$dir"/Hospital.kml
fi

This code takes the input variables and places them within code and then inserts the block of code into the KML file at the bottom, just prior to the last 2 lines of code. ${line} was calculated previously to find the second to last line.

This is an excerpt of the file it is being placed in:

    <Placemark>
      <name>A_FAKE_HOSP_NAME</name>
      <description>FAKE CITY, ST FAKEID</description>
      <styleUrl>#style1</styleUrl>
      <Point>
        <coordinates>-83.12345678,33.123456789,0</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>ANOTHER_HOSP_NAME</name>
      <description>NEW CITY, ST FAKEID2 FAAID1</description>
      <styleUrl>#style1</styleUrl>
      <Point>
        <coordinates>-83.12345678,33.123456789,0</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>FAKE_HOSP</name>
      <description>A CITY, ST FAKEID3</description>
      <styleUrl>#style1</styleUrl>
      <Point>
        <coordinates>-84.12345678,33.123456789,0</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

What I'm trying to accomplish is to take the entire block from <Placemark></Placemark> and find where it would be inserted into the destination file that has thousands of entries. Ideally, the code would take the $HOSP and find where it belongs alphabetically within the destination file, and then insert the entire block <Placemark></Placemark> where it goes in the file.

I appreciate in advance any and all help or suggestions. This script is executed on macOS.

w113msh
  • 3
  • 1
  • 1
    You're better off using XML-aware tools to both insert the new tag and sort the results. An XSLT processor, probably. – Shawn Aug 20 '21 at 21:36
  • 1
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Aug 20 '21 at 21:36

0 Answers0