|
Home > Archive > Unix Shell > May 2004 > sed help
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hello, I have the following file:
$ cat test.txt
<area shape="circle" coords="48,14,30" alt="aaa" title="taaa"><area
shape="circle" coords="48,14,30" alt="bbb" title="tbbb">
And when I run:
$ sed -e 's/alt="\(.*\)"/alt=""/g' test.txt
<area shape="circle" coords="48,14,30" alt="">
My question is why sed doesn't just replace "aaa" and "bbb" with ""? And
how to do it?
| |
| Icarus Sparry 2004-02-16, 6:34 am |
| On Mon, 16 Feb 2004 14:32:45 -0500, Nick wrote:
> Hello, I have the following file:
>
> $ cat test.txt
> <area shape="circle" coords="48,14,30" alt="aaa" title="taaa"><area
> shape="circle" coords="48,14,30" alt="bbb" title="tbbb">
>
> And when I run:
>
> $ sed -e 's/alt="\(.*\)"/alt=""/g' test.txt
> <area shape="circle" coords="48,14,30" alt="">
>
> My question is why sed doesn't just replace "aaa" and "bbb" with ""? And
> how to do it?
This is because "regular expressions are greedy". You are asking for
something which matches
alt="
followed by anything, followed by a double quote. In this case "anything
is
aaa" title="taaa"><area.....title="tbbb
If you replace .* with [^"]*, which is anything except double quotes, you will
get what you want.
As a side note, there is no need to put \( and \) around what you are matching.
This causes sed to remember what it matched, so you can put it back, but as you
are just replacing stuff, you don't need to remember what it was.
| |
| Chris F.A. Johnson 2004-02-16, 7:34 am |
| On Mon, 16 Feb 2004 at 19:32 GMT, Nick wrote:
> Hello, I have the following file:
>
> $ cat test.txt
><area shape="circle" coords="48,14,30" alt="aaa" title="taaa"><area
> shape="circle" coords="48,14,30" alt="bbb" title="tbbb">
>
> And when I run:
>
> $ sed -e 's/alt="\(.*\)"/alt=""/g' test.txt
><area shape="circle" coords="48,14,30" alt="">
>
> My question is why sed doesn't just replace "aaa" and "bbb" with ""? And
> how to do it?
Because the regular expression is greedy; it will match the longest
possible string, which is
alt="aaa" ...... title="tbbb"
You need to tell it to match everything except a double quote
before the second double quote:
sed -e 's/alt="[^"]*"/alt=""/g' test.txt
--
Chris F.A. Johnson http://cfaj.freeshell.org
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Peter Lee 2004-02-16, 7:34 am |
| >>>> Icarus Sparry writes:
Icarus> This is because "regular expressions are greedy". You are
Icarus> asking for something which matches alt=" followed by
Icarus> anything, followed by a double quote. In this case
Icarus> "anything is aaa" title="taaa"><area.....title="tbbb If
Icarus> you replace .* with [^"]*, which is anything except double
Icarus> quotes, you will get what you want.
Do you know of a page that describes the regular expression syntax
used by sed? I find myself struggling with greedy/non-greedy
operators as they are different than what I'm used to in Emacs.
Is sed RE compatible with PERL RE?
| |
| joe@invalid.address 2004-02-16, 8:34 am |
| Peter Lee <pete_lee@swbell.net> writes:
>
> Icarus> This is because "regular expressions are greedy". You are
> Icarus> asking for something which matches alt=" followed by
> Icarus> anything, followed by a double quote. In this case
> Icarus> "anything is aaa" title="taaa"><area.....title="tbbb If
> Icarus> you replace .* with [^"]*, which is anything except double
> Icarus> quotes, you will get what you want.
>
> Do you know of a page that describes the regular expression syntax
> used by sed? I find myself struggling with greedy/non-greedy
> operators as they are different than what I'm used to in Emacs.
http://www.opengroup.org/onlinepubs...p09.html#tag_09
> Is sed RE compatible with PERL RE?
Not entirely.
Joe
--
"I didn't really say everything I said."
- Yogi Berra
| |
| Carlos J. G. Duarte 2004-02-16, 11:33 pm |
| Peter Lee wrote:
> Do you know of a page that describes the regular expression syntax
> used by sed? I find myself struggling with greedy/non-greedy
> operators as they are different than what I'm used to in Emacs.
Sed uses the basic regular expressions (BRE), or sort of, any way.
They are always greed, and very basic:
c char itself, unless special
.. any char
[set] any in set
[^set] any not in set
\( any stuff \) group stuff (will be available as \1 \2 etc)
* applyied to any of the above: zero or more of it, and greedy
This is what you can count with sed (and the ^$ of course).
\+ \? \{n,m\} (variants n; n,m; n,; ,m) can exist...
--
carlos ** http://cgd.sdf-eu.org
| |
|
|
| Alan Connor 2004-02-18, 12:34 pm |
| On Wed, 18 Feb 2004 17:39:35 -0700, Kevin Rodgers <ihs_4664@yahoo.com> wrote:
>
>
>
>
> --
> Kevin Rodgers
> joe@invalid.address wrote:
>
> Nor with awk; see
> http://safari.oreilly.com/JVXSL.asp...nut-ch-6-sect-2
>
There's a seperate library for PERL RE's. You can use things
like \S. Big whoopee.
Package: libpcre3
Priority: important
Section: libs
Installed-Size: 152
Maintainer: Mark Baker <mbaker@iee.org>
Architecture: i386
Source: pcre3
Version: 3.4-1.1
Depends: libc6 (>= 2.2.3-7)
Filename: pool/main/p/pcre3/libpcre3_3.4-1.1_i386.deb
Size: 58856
MD5sum: 5d1635e59f8f2d0c1f6d0363745e5100
Description: Philip Hazel's PERL Compatible Regular Expression library
This is a library of functions to support regular expressions whose syntax
and semantics are as close as possible to those of the PERL 5 language.
That's a couple of years old.
Extended Regular Expressions work just fine. Piss on Perl.
Perl and the GUI can get married and run off to Venus for all I care.
Good riddance.
AC
| |
| joe@invalid.address 2004-02-18, 12:34 pm |
| Alan Connor <zzzzzz@xxx.yyy> writes:
> On Wed, 18 Feb 2004 17:39:35 -0700, Kevin Rodgers
> <ihs_4664@yahoo.com> wrote:
>
> There's a seperate library for PERL RE's. You can use things
> like \S. Big whoopee.
>
> Package: libpcre3
> Priority: important
> Section: libs
> Installed-Size: 152
> Maintainer: Mark Baker <mbaker@iee.org>
> Architecture: i386
> Source: pcre3
> Version: 3.4-1.1
> Depends: libc6 (>= 2.2.3-7)
> Filename: pool/main/p/pcre3/libpcre3_3.4-1.1_i386.deb
> Size: 58856
> MD5sum: 5d1635e59f8f2d0c1f6d0363745e5100
> Description: Philip Hazel's PERL Compatible Regular Expression library
> This is a library of functions to support regular expressions whose syntax
> and semantics are as close as possible to those of the PERL 5 language.
>
> That's a couple of years old.
>
> Extended Regular Expressions work just fine. Piss on Perl.
>
> PERL and the GUI can get married and run off to Venus for all I care.
>
> Good riddance.
The page he referenced didn't show comparisons of PERL with other
things, nor did he mention it (he mentioned awk).
The word "perl" is on the page in a side menu pointing to something
else. Is that enough to invalidate the whole page?
Joe
--
"I didn't really say everything I said."
- Yogi Berra
| |
| Bob Kryger 2004-05-31, 6:06 pm |
| Stephane CHAZELAS wrote:
> 2004-05-27, 12:07(-04), Barry Margolin:
> [...]
>
>
>
> In Bourne-like shell, \ is not a special character inside single
> quotes. There's no way to escape a quote character inside
> quotes, you have first to get out of them. Here, you'd better
> use double quotes:
>
> sed "s/^.*='\(.*\)'\$/\1/" Input > Result
>
also - non sed/awk
cut -f2 -d' <infile >outfile
|
|
|
|
|