[ask]how does sed -e 's/<[^>]*>//g' works?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [ask]how does sed -e 's/<[^>]*>//g' works?
# 1  
Old 10-01-2010
[ask]how does sed -e 's/<[^>]*>//g' works?

I found this command, sed -e 's/<[^>]*>//g', will return the content of a line with pattern something like this, <tag1>content</tag1>..

How does this works?
What does sed -e 's/<[^>]*>//g' actually do?
What if I wanted to get content of a line with pattern something like this, [tag2]content[/tag2]?
thanks..
# 2  
Old 10-01-2010
I don't think that will do what you describe.

Code:
 sed -e 's/pattern/replacement/g'

is a search and replace pattern. In your example, there's no replace, and nothing will be returned.

You could use something like
Code:
sed -e 's/<\([^>]*\)>/\1/g'

to capture what's between the literal <>'s and return it.

The way that works: it could be read "find a literal < followed by 0 or more characters that are not a >, followed by a literal >, capturing the non-'>' characters and replacing the entire match with what is captured, for all instances (the meaning of the g at the end)
This User Gave Thanks to malcolmpdx For This Post:
# 3  
Old 10-01-2010
Quote:
Originally Posted by malcolmpdx
I don't think that will do what you describe.

Code:
 sed -e 's/pattern/replacement/g'

is a search and replace pattern. In your example, there's no replace, and nothing will be returned.

You could use something like
Code:
sed -e 's/<\([^>]*\)>/\1/g'

to capture what's between the literal <>'s and return it.

The way that works: it could be read "find a literal < followed by 0 or more characters that are not a >, followed by a literal >, capturing the non-'>' characters and replacing the entire match with what is captured, for all instances (the meaning of the g at the end)
thanks for answering..
when I run your command, I got different result..
Code:
echo "<tag1>content</tag1>" | sed -e 's/<\([^>]*\)>/\1/g'
tag1content/tag1

and when I run this command, I got this..
Code:
echo "<tag1>content</tag1>" | sed -e 's/<[^>]*>//g'
content

Your explanation is really helpful, because I was wondering what [, ^>, and *> means in sed..
# 4  
Old 10-01-2010
Alternatives Smilie
Code:
# echo "<tag1>content</tag1>" | sed -e 's/<[^<]*>//g'
content

Code:
# echo "<tag1>content</tag1>" | sed -e 's/.*>\(.*\)<.*/\1/'
content

Code:
# echo "<tag1>content</tag1>" | sed -e 's/<[^<]*>//1'
content</tag1>

Code:
# echo "<tag1>content</tag1>" | sed -e 's/<[^>]*>//2'
<tag1>content

-- PowerFull Sed --
This User Gave Thanks to ygemici For This Post:
# 5  
Old 10-01-2010
Quote:
Originally Posted by 14th
I found this command, sed -e 's/<[^>]*>//g', will return the content of a line with pattern something like this, <tag1>content</tag1>..

How does this works?
What does sed -e 's/<[^>]*>//g' actually do?
What if I wanted to get content of a line with pattern something like this, [tag2]content[/tag2]?
thanks..
The first sentence removes all the tags so the content remains.
Code:
$ echo "<tag1>content</tag1>" | sed 's/<[^>]*>//g'
content

To do the same with [] you could do this:
Code:
$ echo "[tag2]content[/tag1]" | sed 's/\[[^]]*\]//g'
content

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-01-2010
Huh. I could have sworn I got different results when I tested this. Now, though, it behaves as you describe. I will claim tiredness as my excuse.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed works on Linux but fails on Solaris

Hi, On Linux i get the desired ouput: echo "<value>WEB_USER</value>" | sed 's/\(<value>\|<\/value>\)//g'Output: Executing the same command on Solaris: echo "<value>WEB_USER</value>" | sed 's/\(<value>\|<\/value>\)//g'Output: I need to get the desired output on Solaris i.e. WEB_USER and... (4 Replies)
Discussion started by: mohtashims
4 Replies

2. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

3. Shell Programming and Scripting

SED command works in terminal, but not Applescript

The following command works perfectly in Terminal, but not in Applescript. (Returns "unknown token" error for square brackets.) (new to site. sorry.) I have an Applescript that is designed to find and remove any square-bracketed text, including the square brackets. I ran the following code from... (1 Reply)
Discussion started by: Phillip Acosta
1 Replies

4. Shell Programming and Scripting

Sed script not working properly on Solaris (works fine on AIX)?

Hi, I have a problem with a SED script that works fine on AIX but does not work properly on a Solaris system. The ksh script executes the SED and puts the output in HTML in tables. But the layout of the output in HTML is not shown correctly(no tables, no color). Can anyone tell if there is... (7 Replies)
Discussion started by: Faith111
7 Replies

5. Shell Programming and Scripting

sed command works on Fedora/Ubuntu, but doesn't work in Mac

Hi, I have a question. I define a function using sed command: replace() { searchterm=$1 replaceterm=$2 sed -e "s/$searchterm/$replaceterm/ig" $3 > $WORK'tempfile.tmp' mv $WORK'tempfile.tmp' $3 } Then I call replace 'test = 0' 'test = 1' $myfile This code... (1 Reply)
Discussion started by: Dark2Bright
1 Replies

6. Shell Programming and Scripting

sed command works on Fedora/Ubuntu, but doesn't work in Mac

Hi, I have a question. I define a function using sed command: replace() { searchterm=$1 replaceterm=$2 sed -e "s/$searchterm/$replaceterm/ig" $3 > $WORK'tempfile.tmp' mv $WORK'tempfile.tmp' $3 } Then I call replace 'test = 0' 'test = 1' $myfileThis code works well in... (1 Reply)
Discussion started by: Dark2Bright
1 Replies

7. Shell Programming and Scripting

sed works on Linux and Unix does not work

Hi, I use this command in Linux but if I run the same command does not work in freebsd. Follow the below command: Linux works: sed -e '1731a\' -e '####' squid.conf > squid2.conf ; sed -e '1731a\' -e 'acl TESTE_ip src 192.168.1.1/255.255.255.255' squid2.conf > squid.conf ; sed -e... (7 Replies)
Discussion started by: andreirp
7 Replies

8. Shell Programming and Scripting

sed command works from cmd line to standard output but will not write to file

Hi all .... vexing problem here ... I am using sed to replace some special characters in a .txt file: sed -e 's/_<ED>_/_355_/g;s/_<F3>_/_363_/g;s/_<E1>_/_341_/g' filename.txt This command replaces <ED> with í , <F3> with ó and <E1> with á. When I run the command to standard output, it works... (1 Reply)
Discussion started by: crumplecrap
1 Replies

9. Shell Programming and Scripting

Delete rest of line with sed works on Linux but not on Solaris

Hi all, Our application installation uses "sed" command to delete rest of line. It work perfect on Linux but fail on Solaris. The OS versions are Solaris 9 and Linux Red Hat AS 3. yourfile.txt hello and world cat and dog hello world in linux: cat yourfile.txt | sed ‘s/\(\+\)... (3 Replies)
Discussion started by: javac2005
3 Replies

10. Shell Programming and Scripting

Appending line with sed works on Linux but not on Solaris

Hi folks, Our application installation uses "sed" command to append string after specific line or after line number. Both cases work perfect on Linux but fail on Solaris. The OS versions are Solaris 9 and Linux Red Hat AS 3. i.g: Linux: ----- file foo.txt aaa bbb ccc ddd root#... (4 Replies)
Discussion started by: nir_s
4 Replies
Login or Register to Ask a Question