-   Programming (
-   -   Sed pattern replacement (

icyrail 10-26-2009 06:52 PM

Sed pattern replacement
Hi, this is my first post, but I have benefited a lot from people who have previously posted on the forum. So thank you people!

I want to replace vowel pairs with the second vowel of the pair whenever the vowel pair occurs before a symble !.

For example:

I want to change the word "meekerpaat!niiyah" to "mekerpat!niiyah".

I have tried

echo meekerpaat!niiyah|sed "[aeiou]\([aeiou]\)\(\!*\)/\1\2/g"

and variations of the code, playing around with the use of nth instances as well as adding another \! in front of the * symbol. The results are varied.

How should I go about it?

Thank you in advance.

neonsignal 10-26-2009 11:09 PM

There were a couple of typos above, but I think you were saying you had tried:


echo 'meekerpaat!niiyah' | sed 's/[aeiou]\([aeiou]\)\(.*\!\)/\1\2/g'
The problem with this is that it will only replace the first vowel pair, because the match will swallow everything up to the exclamation mark, and then continue on from that point. And it has to do this even to determine if the exclamation mark exists. So you get the result 'mekerpaat!niiyah'.

You can put a loop in the sed command, so that it repeats the replace command until no more are found:


echo 'meekerpaat!niiyah' | sed ':repeat; s/[aeiou]\([aeiou]\)\(.*\!\)/\1\2/; t repeat'
This will give you the desired result 'mekerpat!niiyah'.

ghostdog74 10-26-2009 11:46 PM

without messy regex

echo 'meekerpaat!niiyah' | awk -vFS= -vOFS= '
 for(i=1;i<=RSTART;i++){ if( $i == $(i+1)){ $i="" } }


$ ./

icyrail 10-27-2009 06:33 AM

Hey, you guys are awesome. I haven't heard of the 'repeat' command, and could not possibly have done it with such a neat code.

Thank you neonsignal as well. I'll think about the code some day when I go into awk.

You really made my life easier!

All times are GMT -5. The time now is 03:42 AM.