Remove strings within range using sed


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

Hey folks

I have a big file that contains junk data between the tags <point> and </point> and I need to delete it (including `<point>' and `</point>').

i.e.
Code:
a = 1
<point>
 123123
 2342352
 234231
 234256
</point>
print a

needs to become
Code:
a = 1
print a

I'm certain that this is a one-liner in sed, but I'm not familiar enough with the language to know how to do it.
# 2  
Old 07-24-2010
Hi

Code:
sed '/point/,/\/point/d' file

Guru.
# 3  
Old 07-24-2010
Code:
# sed -n '1p;$p' infile
a = 1
print a

# 4  
Old 07-24-2010
Code:
sed -n '/point/,/\/point/!p' file

# 5  
Old 07-24-2010
MySQL

Code:
# sed -n '/[a-z][^<point>]/p' infile
a = 1
print a



---------- Post updated at 01:13 AM ---------- Previous update was at 01:05 AM ----------

Code:
# sed '/<point>/,${;/print/!d}' infile
a = 1
print a


Last edited by ygemici; 07-24-2010 at 07:23 PM..
# 6  
Old 07-24-2010
Code:
#!/bin/bash

declare -i flag
flag=0
while read -r LINE
do
  case "$LINE" in
   *"</point>"*)
      LINE=${LINE##*</point>}
      flag=0
      ;;
   *"<point>"*)
      LINE=${LINE%%<point>*}
      echo "$LINE"
      flag=1
      ;;
  esac
  [[ $flag = 1 ]] && continue
  [[ $flag = 0 ]] && echo "$LINE"
done < "file"

Code:
# cat file
a = 1
some text here , don't delete <point>
 123123
 2342352
 234231
 234256
</point> some text here  too that cannot be deleted
print a

linux$ ./myscript.sh
a = 1
some text here , don't delete
 some text here  too that cannot be deleted
print a

# 7  
Old 07-25-2010
Thanks everyone.

That works perfectly well for the filetype I mentioned. Now, the problem is that I have to take out the line immediately preceding the first pattern as well.

That is, if the file is
Code:
a = 1
b = 1
<point>
124134
123123
42352
</point>
print a
c = 1

I need
Code:
a = 1
print a
c = 1

Can you suggest the modification?
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