LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-05-2012, 06:00 AM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Regex to Search and Replace Repeatedly


Hi,


Using Regex / Regular Expressions, I want to replace all the instances of 123 with 12 and the final data-set/result should not contain any 123 in it. Example:

Code:
[demo@devhost ~]$ cat data
123333
123123456 123333 12333
[demo@devhost ~]$ cat data | sed 's/123/12/g'
12333
1212456 12333 1233
[demo@devhost ~]$
You can see that there are still instances of 123. So, how do we make sure that the final output would not contain 123?
 
Old 10-05-2012, 06:14 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Is this what you are looking for:
Code:
sed 's/123\{1,\}/12/g' data 
12
1212456 12 12
The {1,} part tells sed to look for one or more 3's.
 
Old 10-05-2012, 06:31 AM   #3
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by druuna View Post
Is this what you are looking for:
Code:
sed 's/123\{1,\}/12/g' data 
12
1212456 12 12
The {1,} part tells sed to look for one or more 3's.

Great, man! I just missed that point! Thanks!
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Emacs regex headache - replace between ' and ' ?? rylan76 Programming 8 10-20-2011 09:08 AM
[SOLVED] Simple regex match without replace? nrg Programming 1 11-21-2009 03:04 AM
Perl regex - search and replace duplicate chars PAix Programming 10 12-18-2007 03:19 AM
PHP regex replace? iluvatar Programming 5 09-10-2004 10:03 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration