Filter a .kml file (xml) to remove unwanted entries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter a .kml file (xml) to remove unwanted entries
# 1  
Old 05-10-2011
Filter a .kml file (xml) to remove unwanted entries

Ok, i have a .kml file that that i want to trim down and get rid of the rubbish from. its formatted like so:

Code:
<Placemark>
<name><![CDATA[BTHomeHub****]]></name>
<description><![CDATA[BSSID: <b>*********</b><br/>Capabilities: <b>[WPA-PSK-TKIP]</b><br/>Frequency: <b>2437</b><br/>Timestamp: <b>1304892397000</b><br/>Date: <b>2011-05-08 23:06:37</b>]]></description><styleUrl>#red</styleUrl>
<Point>
<coordinates>******************</coordinates></Point>
</Placemark>

there are about 1200 of these place mark tags within the file.

What i want to do is remove the entire placemark tag if the name tag contains a set string. eg, BTHomehub

I know this CAN be done, but i literally have no idea how. my shell scripting experience is lacking to say the least. Even a point in the right direction is more than welcome, thanks in advance for any help!
Nathan
# 2  
Old 05-10-2011
Try:
Code:
perl -p0e 's/<Placemark>.*?BTHomeHub.*?<\/Placemark>\n//sg' file

# 3  
Old 05-10-2011
thankyou for the speedy reply bartus. For some reason this isnt working, It returns the file with 5 placemark tags, none of which are the BTHomeHub ones (which is correct), but there should be a bunch more.

I thought maybe it was just outputting the first few so i ran
Code:
perl -p0e 's/<Placemark>.*?BTHomeHub.*?<\/Placemark>\n//sg' file | gedit

to pipe the output to a txt file but i just get a blank page.

Any ideas? Also could you explain what that line actually does? what does -p0e mean?
# 4  
Old 05-10-2011
I think you should show us more sample entries (like first 50 or something).
edit: nevermind, I know what is wrong. I'll try to get you right solution soon.
edit2: Try this:
Code:
perl -p0e 's/<Placemark>\n[^\n]+BTHomeHub.*?<\/Placemark>\n//sg' file


Last edited by bartus11; 05-10-2011 at 04:07 PM..
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 05-10-2011
Not a one liner but does the job

Code:
#!/bin/bash
i=1;f=0
while read line
do
  if [ "$line" == "<Placemark>" ]; then s=0; fi
  echo "$line" | grep "$1" >/dev/null 2>&1
  if [ $? -eq 0 ] && [ $f -eq 0  ] ;then f=1;fi
  if [ "$line" == "</Placemark>" ]; then s=1; fi
  x[$i]="$line"
  if [ $s -eq 1 ];then
    if [ $f -eq 0 ];then
      for((j=1;j<=i;j++))
      do
        echo ${x[$j]}
      done
    fi
    i=0;unset x;f=0
  fi
  ((i=i+1))
done < infile

Usage : script pattern

regards,
Ahamed

Last edited by ahamed101; 05-10-2011 at 05:24 PM..
# 6  
Old 05-10-2011
That works perfectly! Thankyou.

Now.... I assumed i would just replace 'BTHomeHub' with any other set string within the <placemark> tags to remove said tag, but that doesnt work. How are you telling perl to only search the name line?

I was going to remove the placemarks that are 'wpa-psk-tkip' enabled.
# 7  
Old 05-10-2011
Try the shell script.

Usage : script pattern_to_be_removed

eg: ./script "wpa-psk-tkip"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove unwanted commas from a .csv file?

how to remove unwanted commas from a .csv file Input file format "Server1","server-PRI-Windows","PRI-VC01","Microsoft Windows Server 2012, (64-bit)","Powered On","1,696.12","server-GEN-SFCHT2-VMS-R013,server-GEN-SFCHT2-VMS-R031,server-GEN-SFCHT2-VMS-R023"... (5 Replies)
Discussion started by: ranjancom2000
5 Replies

2. Shell Programming and Scripting

Filter file to remove duplicate values in first column

Hello, I have a script that is generating a tab delimited output file. num Name PCA_A1 PCA_A2 PCA_A3 0 compound_00 -3.5054 -1.1207 -2.4372 1 compound_01 -2.2641 0.4287 -1.6120 3 compound_03 -1.3053 1.8495 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Removing unwanted tags from xml file

I have a XML file given as below: "<ProductUOMAlternativeDetails> <removetag> <UOMCode>EA</UOMCode> <numeratorForConversionToBaseUOM>1</numeratorForConversionToBaseUOM> <denominatorForConversionToBaseUOM>1</denominatorForConversionToBaseUOM> <length>0.59</length> <width>0.96</width> ... (3 Replies)
Discussion started by: vikingh
3 Replies

4. Shell Programming and Scripting

Filter a .kml file (xml) with data set from text file

I have a .kml file. So I want filter the .kml to get only the tags that have this numeric codes that they are in a text file 11951 11952 74014 11964 11965 11969 11970 11971 11972 60149 74018 74023 86378 11976 11980 11983 11984 11987 (5 Replies)
Discussion started by: pcoj33
5 Replies

5. Shell Programming and Scripting

Filter/remove duplicate .dat file with certain criteria

I am a beginner in Unix. Though have been asked to write a script to filter(remove duplicates) data from a .dat file. File is very huge containig billions of records. contents of file looks like 30002157,40342424,OTC,mart_rec,100, ,0 30002157,40343369,OTC,mart_rec,95, ,0... (6 Replies)
Discussion started by: mukeshguliao
6 Replies

6. Shell Programming and Scripting

Remove unwanted lines

I have a .xml file, where i need some output. The xml file is like: Code: <?******?></ddddd><sssss>234</dfdffsdf><sdhjh>534</dfdfa>......... /Code I need the output like: code 234 534 . . . /code How can i do it? (5 Replies)
Discussion started by: anupdas
5 Replies

7. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

8. Shell Programming and Scripting

Remove unwanted XML Tags

I have set of sources and the respective resolution. Please advice how to resolve the same using Unix shell scripting. Source 1: ======= <ext:ContactInfo xmlns:ext="urn:AOL.FLOWS.Extensions"> <ext:InternetEmailAddress>AOL@AOL.COM</ext:InternetEmailAddress> </ext:ContactInfo> Resoultion... (1 Reply)
Discussion started by: ambals123
1 Replies

9. Shell Programming and Scripting

Filter unwanted lines

Hi All, I have the below input and i only want to filter out some un-wanted info from here. Expected output is below. Can somebody help ? The catch is that i want to grep those lines with term "k=" and lines with term "**" as the 1st column and "07" as the last column. And the number of... (15 Replies)
Discussion started by: Raynon
15 Replies

10. Shell Programming and Scripting

Remove unwanted data?

Hi Can any one help me remove the unwanted data? I would want to remove the complete event id 4910 ( the type there is INFO), that means, I have to remove starting from 7th - 19th lines. can any one of you please help? Thanks, (24 Replies)
Discussion started by: hitmansilentass
24 Replies
Login or Register to Ask a Question