Sed like substitutions 
Author Message
 Sed like substitutions

Hi

I have a problem that my book doesn't tell me how to solve. I want to do
some sed style substitutions using back references, such as:

s/foo\([0-9][0-9]*\)bar/Number \1/

for instance.

I know that extended regexps replece \(, \) with (, ), and I know that
back references seem to work[*]

But if I do (in awk):

sub(/foo([0-9]+)bar/, "Number \1")

the \1 seems not to produce anything.

I really want to be able to do these sed style substitutions. Can anyone
help?

BTW, I'm using all GNU tools.

[*]I tried the following in egrep:

egerp '([0-9])abc\1'

and it worked matching 0abc0 1abc1 but not 1abc0

Thanks

-Ed

--
Konrad Zuse should  recognised. He built the first      | Edward Rosten
binary digital computer (Z1, with floating point) the   | Engineer

commercial one (Z4).                                    | eng.ox.ac.uk



Mon, 21 Apr 2003 09:50:15 GMT  
 Sed like substitutions

Quote:

> I have a problem that my book doesn't tell me how to solve. I want to do
> some sed style substitutions using back references, such as:

> s/foo\([0-9][0-9]*\)bar/Number \1/

> for instance.

> I know that extended regexps replece \(, \) with (, ), and I know that
> back references seem to work[*]

> But if I do (in awk):

> sub(/foo([0-9]+)bar/, "Number \1")

> the \1 seems not to produce anything.

> I really want to be able to do these sed style substitutions. Can anyone
> help?

> BTW, I'm using all GNU tools.

In gawk (GNU awk), use gensub(). See gawk(1) for details.

--
Jim Monty

Tempe, Arizona USA



Mon, 21 Apr 2003 08:21:43 GMT  
 Sed like substitutions

Quote:


> > I have a problem that my book doesn't tell me how to solve. I want to do
> > some sed style substitutions using back references, such as:

> > s/foo\([0-9][0-9]*\)bar/Number \1/

> > for instance.

> > I know that extended regexps replece \(, \) with (, ), and I know that
> > back references seem to work[*]

> > But if I do (in awk):

> > sub(/foo([0-9]+)bar/, "Number \1")

> > the \1 seems not to produce anything.

> > I really want to be able to do these sed style substitutions. Can anyone
> > help?

> > BTW, I'm using all GNU tools.

> In gawk (GNU awk), use gensub(). See gawk(1) for details.

Thankyou very much. It worked perfectly after I figured out to use \\1
instead of \1.

I'd like to thank you again for such a quick response.

Is gensub() limited to gawk only? If it is, is there a portable way to
do sed style substitutions.

Thanks

-Ed

--
Konrad Zuse should  recognised. He built the first      | Edward Rosten
binary digital computer (Z1, with floating point) the   | Engineer

commercial one (Z4).                                    | eng.ox.ac.uk



Mon, 21 Apr 2003 10:26:01 GMT  
 Sed like substitutions

<snip>

Quote:
>Is gensub() limited to gawk only? If it is, is there a portable way to
>do sed style substitutions.

Unfortunately gensub() is limited to gawk. It's possible to write
functions in awk that use match() and substr() to provide functionality
similar to parenthesized regex memory, but it's rather complicated. At
least it's portable. There should be some examples in the Deja archive
of comp.lang.awk.


Mon, 21 Apr 2003 13:16:42 GMT  
 Sed like substitutions

Quote:

> Is gensub() limited to gawk only?

Yes.

Quote:
> If it is, is there a portable way to do sed style substitutions?

No.

In general, awk does not support parenthesized subexpression memory.
And in the specific cases where it is supported, it is within the
context of a substitution operation only.

MKS awk supports backreferences in its sub() and gsub() functions:

C:/>echo foo123bar | awk '{ sub(/foo([0-9]+)bar/, "Number \1"); print }'
Number 123
C:/>

(N.B. The sub() function in the script above is identical to the
one you proposed in your original post. I "cut and pasted" it.)

If you need "sed-style substitutions", use sed. If you need them
within the context of a programming language, use Perl:

$ echo foo123bar | perl -pe 's/foo(\d+)bar/Number $1/'
Number 123
$

An added benefit of using Perl is its more general form of
parenthesized subexpression memory:

$ perl -le 'print "Number $1" if "foo123bar" =~ /foo(\d+)bar/'
Number 123
$ perl -le '($num) = "foo123bar" =~ /foo(\d+)bar/; print "Number $num"'
Number 123
$

--
Jim Monty

Tempe, Arizona USA



Mon, 21 Apr 2003 15:07:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. tr, sed, and substitution speed

2. Microsoft likes us :)

3. Looking for someone who likes to write reports

4. Greg likes Oberon (response)

5. Summary of Seek ELIZA-liked Source Codes!!!

6. Q: EAI likes BLUE ??

7. Sun compiler not liking X in format statements

8. Web interface to Majordomo-likes?

9. Who likes Info files?

10. I liked TCL, i love LUA

11. Likes/Dislikes of LISP

12. Liked list question

 

 
Powered by phpBB® Forum Software