Thread: Unix Tools
View Single Post

   
  #16 (permalink)  
Old 01-17-2008, 08:36 AM
Alan Connor
 
Posts: n/a
Default Re: Unix Tools

On Fri, 24 Oct 2003 18:55:14 +0200, Stephane CHAZELAS <this.address@is.invalid> wrote:
>
>
> 2003/10/24, 10:12(-05), Ed Morton:
> [...]
>> You never need to do pipe things between sed and awk since awk
>> can do anything sed can do.

> [...]
>
> Not always. Even GNU awk can't do easily things that sed can
> do in one line.
>
> sed 's/\(.\)\1/2\1/3'
>
> replaces the third occurrence of two consecutive same chars 'X'
> with "2X" (may not work with some old seds).
>
> The same in awk:
>
> awk '{
> line=$0; l=length; n=3; o=""
> for (i=1;i<=l;i++) {
> c=substr($0,i,1);
> if (c == o) {
> if (!--n) {
> line=substr($0,1,i-2) "2" substr($0, i)
> break
> }
> o=""
> } else
> o=c
> }
> print line}'
>
>
> gawk '{print gensub(/(.)\1/,"2\\1",3)}'
>
> doesn't work.
>
> There's no equivalent for 'y' in awk.
>
> --
> Stéphane ["Stephane.Chazelas" at "free.fr"]



Way to go Stephane! This is going in my sed docs for the next time
the awk/sed argument comes up.

--
Alan C
Post validation at http://tinyurl.com/rv0y
Reply With Quote