LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-26-2009, 05:52 PM   #1
icyrail
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, 10:09 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

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

Rep: Reputation: 240Reputation: 240Reputation: 240
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
 
Old 10-27-2009, 05:33 AM   #4
icyrail
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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!
 
  


Reply

Tags
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 08:01 PM
How to get the pattern using sed or awk? ahpin Programming 3 08-02-2007 03:16 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 04:40 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM


All times are GMT -5. The time now is 08:35 PM.

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