Remove strings within range using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove strings within range using sed
# 8  
Old 07-25-2010
Hi

Code:
awk 'NR!=1 && !f{ if($0 ~ /point/)f=1;else if(x)print x;}{if ($0 ~ /\/point/){f=0;x="";}else x=$0}END{print x}' f=0 file

Guru.
# 9  
Old 07-25-2010
Thanks Guru. I ran the awk command you suggested. It certainly removes what I wanted, but it also seems to be knocking out some other parts of the file, that are essential.

I tried presenting the problem at its simplest, so as not to burden you guys with unnecessary complications, but I see that it would be better if I explained the whole thing out.

The file that I am trying to edit is a KML file. It stores GPS coordinates and is interpreted by both Google Earth and Google Maps to draw maps. The file contains many folders, some called `Waypoints', some called `Tracks', some called `Points' etc. The `Points' folder contains extra waypoints that do not form a part of the track. These only clutter up the map, so I want to nuke the entire Points folder. Sadly, the declaration of folder names is slightly clumsy in KML. A file looks like this

Code:
<Folder>
  <name>Waypoints</name>
  # SOME ESSENTIAL STUFF
  # SOME ESSENTIAL STUFF
</Folder>
<Folder>
  <name>Tracks</name>
  # SOME ESSENTIAL STUFF
  # SOME ESSENTIAL STUFF
  <Folder>
    <name>Points</name>
    # JUNK
    # JUNK
    # JUNK
  </Folder>
  # SOME ESSENTIAL STUFF
</Folder>

What I need to remove is all instances of the Points folder (there are more than one), i.e. from <Folder> to </Folder> when the name tag value is `Points'. Another way to look at it would be to remove <Folder> to </Folder> when the 1st line after <Folder> reads <name>Points</name>.

I hope this helps

Thanks in advance!

---------- Post updated at 10:07 PM ---------- Previous update was at 11:45 AM ----------

No joy?

:-(
# 10  
Old 07-25-2010
Hi,

try:

Code:
awk '/Folder>/{s=$0;t=getline;if ($0 ~ /Points/){f=0} else {if (t) print s;f=1}}f' file

Output:
Code:
<Folder>
  <name>Waypoints</name>
  # SOME ESSENTIAL STUFF
  # SOME ESSENTIAL STUFF
</Folder>
<Folder>
  <name>Tracks</name>
  # SOME ESSENTIAL STUFF
  # SOME ESSENTIAL STUFF
  </Folder>
  # SOME ESSENTIAL STUFF
  </Folder>

HTH Chris
# 11  
Old 07-26-2010
Hi Cristoph

That takes out what I need, but again, it seems to delete parts of the script that it isn't meant to. For some reason, it removes the first 100 odd lines of script as well. I would post the input and output files, but they're huge. Would it help if you showed you both?

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match patterns between two files and extract certain range of strings

Hi, I need help to match patterns from between two different files and extract region of strings. inputfile1.fa >l-WR24-1:1 GCCGGCGTCGCGGTTGCTCGCGCTCTGGGCGCTGGCGGCTGTGGCTCTACCCGGCTCCGG GGCGGAGGGCGACGGCGGGTGGTGAGCGGCCCGGGAGGGGCCGGGCGGTGGGGTCACGTG... (4 Replies)
Discussion started by: bunny_merah19
4 Replies

2. UNIX for Beginners Questions & Answers

Search for Multiple strings in a given date range and print the Group if they exists

Hi, I am Searching for Multiple strings in a given date range and print the Group if they exists. the below is the format: ------------------------------------------------------------------------------------------------------------------------- ID: FIRST ID MESSAGE: Event Message... (5 Replies)
Discussion started by: linuxuser999
5 Replies

3. Shell Programming and Scripting

sed or awk to remove specific column to one range

I need to remove specific column to one range source file 3 1 000123456 2 2 000123569 3 3 000123564 12 000123156 15 000125648 128 000125648 Output required 3 000123456 2 000123569 3 000123564 12 000123156 15 000125648 128 000125648 (6 Replies)
Discussion started by: ranjancom2000
6 Replies

4. Shell Programming and Scripting

USING sed to remove multiple strings/words from a line

Hi I use sed comnand to remove occurance of one workd from a line. However I need to removed occurance of dufferent words in ne line. Original-1 Hi this is the END of my begining Comand sed s/"END"/"start"/g Output-1 Hi this is the start of my beginig But I have more... (9 Replies)
Discussion started by: mnassiri
9 Replies

5. Shell Programming and Scripting

awk or sed script to remove strings

Below am trying to separate FA-7A:1, In output file it should display 7A 1 Command am using Gives same output as below format: 22B7 10000000c9720873 0 22B7 10000000c95d5d8b 0 22BB 10000000c97843a2 0 22BB 10000000c975adbd 0 Not showing FA ports as required format... (5 Replies)
Discussion started by: aix_admin_007
5 Replies

6. Shell Programming and Scripting

Remove a range of lines from a file using sed

Hi I am having some issue editing a file in sed. What I want to do is, in a loop pass a variable to a sed command. Sed should then search a file for a line that matches that variable, then remove all lines below until it reaches a line starting with a constant. I have managed to write a... (14 Replies)
Discussion started by: Andy82
14 Replies

7. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

8. Shell Programming and Scripting

Sed: Remove whitespace between two strings

I have a 13 number string, some whitespace, and then /mp3. I need to join them. Everyline that I need this for begins with "cd" (without the quotes). What it looks like now: cd media/Audio/WAVE/9781933976334 /mp3 What I want my output to be: cd media/Audio/WAVE/9781933976334/mp3 The 13... (7 Replies)
Discussion started by: glev2005
7 Replies

9. Shell Programming and Scripting

sed: remove characters between and including 2 strings

I have the following line: 4/23/2010 0:00:38.000: Copying $$3MSYDDC02$I would like to use sed (or similiar) to remove everthing between and including $ that appears in the line so it ends up like this. 4/23/2010 0:00:38.000: Copying 3MSYDDC02I have been trying these but i'm really just... (5 Replies)
Discussion started by: jelloir
5 Replies

10. Shell Programming and Scripting

remove range part of a file with sed

Hello, I would like to remove a range from a file with sed or any script command that is appropriate The section start by and finish by and I would like to keep line Could you tell me which command I should type ? Thanks a lot, Franck My input file is like this... (1 Reply)
Discussion started by: mfranck
1 Replies
Login or Register to Ask a Question