Using sed to substitute first occurrence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to substitute first occurrence
# 1  
Old 07-08-2008
Using sed to substitute first occurrence

I am trying to get rid of some ending tags but I run into some problems.

Ex.

How are you?</EndTag><Begin>It is fine.</Begin><New> Just about

I am trying to get rid of the ending tags, starts with </ and ending with >. (which is </EndTag> and </Begin>)

I tried the following

sed 's/<\/.*>//g' file

It give me the following result which I did not want.
How are you? Just about.

It eliminated everything between you? and Just. There has to be a way to eliminate up to the first >



Second:

Same problem but split over two line.

How are you?</EndTag><Begin>It is fine.</Beg
in><New> Just about the same

Does anyone have an catchall sed command to eliminate everything from </... until it reaches the first > even though it expands mulitple lines.

Thank you.
# 2  
Old 07-08-2008
sed by default is greedy and matches all instances of “>” up to the very last one on the line. Here is one way of only removing the content between "</" and ">":
Code:
$ cat file
How are you?</EndTag><Begin>It is fine.</Begin><New> Just about
$ sed 's/<\/[^>]*>//g' file
How are you?<Begin>It is fine.<New> Just about
$

# 3  
Old 07-09-2008
Thanks but what about 2nd part

Thanks, that seems to work on the first part.
But what about the 2nd part where the ending tag is split between two lines.
Sed seems to work when it is on one line but not when it is one two lines.

Ex.
</Beg
in>

Still haven't figure that part out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Substitute a character with sed

hi all, i'd like to modify a file with sed , i want to substuite a char "-" with "/" how can i do this? Thanks for all regards Francesco (16 Replies)
Discussion started by: Francesco_IT
16 Replies

2. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

3. UNIX for Dummies Questions & Answers

Substitution in first occurrence with sed

I have the following script: sed '/string1/,/string2/!d' infile I want to apply the script to the first occurrence only. I have tried sed '0,/string1/,/string2/!d' infile Of course, that does not work Any help will be greatly appreciated (12 Replies)
Discussion started by: Xterra
12 Replies

4. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

5. UNIX for Dummies Questions & Answers

Sed, last occurrence

How to find last occurrence of a keyword in a file using sed. (4 Replies)
Discussion started by: nexional
4 Replies

6. Shell Programming and Scripting

sed substitute command -- need help

I am trying to do what I thought should be a simple substitution, but I can't get it to work. File: Desire output: I thought I'd start with a sed command to remove the part of the header line preceding the string "comp", then go on to remove the suffix of the target string (e.g. ":3-509(-)"),... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. UNIX for Dummies Questions & Answers

Using sed to substitute between quotes.

I'm using sed to perform a simply search and replace. The typical data is: <fig><image href="Graphics/BAV.gif" align="left" placement="break" I need to replace the value in the first set of quotes, keeping the remainder of the line the same. Thus: <fig><image href="NEW_VALUE" align="left"... (3 Replies)
Discussion started by: Steve_altius
3 Replies

8. Shell Programming and Scripting

Using SED to substitute between two patterns.

Hi All, I'm currently using SED to make various changes to some .xml files I'm working on, but I'm stuck on this particular problem. I want to remove '<placeholder>element-name</placeholder>' from the following: <heading>Element <placeholder>element-name</placeholder> not... (2 Replies)
Discussion started by: Steve_altius
2 Replies

9. UNIX for Dummies Questions & Answers

sed substitute situation

I am having a problem executing a sed substitute in a file. I have tried alot of different things I found in previous posts, however non seem to work. I want to substitute this in $FILE: VALUE=33.4 In the script I have tried the following: prev=$(awk -F"=" '{ print $2 }' $FILE ) new=$(echo... (16 Replies)
Discussion started by: newbreed1
16 Replies

10. Shell Programming and Scripting

Grep for the same occurrence or maybe Sed

Hi, I have a file that looks like this dasdjasdjoasjdoasjdoa SYN dakspodkapsdka asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf shfishifhsdifhsidhfif fsdfsdfsdfsdfs sdfsdfsdfsdsdfsdfsdff cercercercerce sdasdajsdoajsodasodoo FIN dasdaskdpasdda... (4 Replies)
Discussion started by: hcclnoodles
4 Replies
Login or Register to Ask a Question