Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-26-2009, 06:52 PM   #1
LQ Newbie
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
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.
Old 10-26-2009, 11:09 PM   #2
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Jessie (Fluxbox WM)
Posts: 1,380
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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'.
Old 10-26-2009, 11:46 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 243Reputation: 243Reputation: 243
without messy regex
echo 'meekerpaat!niiyah' | awk -vFS= -vOFS= '
 for(i=1;i<=RSTART;i++){ if( $i == $(i+1)){ $i="" } }
$ ./
Old 10-27-2009, 06:33 AM   #4
LQ Newbie
Registered: Oct 2009
Posts: 2

Original Poster
Rep: Reputation: 0

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!


sed, substitution

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
last pattern with sed? xpto09 Linux - Newbie 6 10-04-2007 09:01 PM
How to get the pattern using sed or awk? ahpin Programming 3 08-02-2007 04:16 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 02:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 05:40 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 08:40 AM

All times are GMT -5. The time now is 01:51 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration