LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Sed pattern replacement (http://www.linuxquestions.org/questions/programming-9/sed-pattern-replacement-764649/)

icyrail 10-26-2009 05: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 10:09 PM

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

Code:

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:

Code:

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 10:46 PM

without messy regex
Code:

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

output
Code:

$ ./shell.sh
mekerpat!niiyah


icyrail 10-27-2009 05:33 AM

Awesome!
 
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 05:52 PM.