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 06-10-2012, 08:28 PM   #1
keirvt
Member
 
Registered: Sep 2006
Location: Sydney Australia
Distribution: fedora/Ubuntu
Posts: 123

Rep: Reputation: 17
sed matching


Sed maching is not behaving for me.

I'm using bash shell
As I understand it plus mean match the previous character or character definition 1 or more times.

I'm look for the first group of digits in a line.
Suppose I wish to change the three digits of the first encountered number

Code:
echo "text etc 123 more text"|sed 's/[0-9][0-9][0-9]/abc/'
text etc abc more text
Everything is as one would expect with the three first numeric changed to "abc".
However trying the more concise syntax fails

Code:
echo "text etc 123 more text"|sed 's/[0-9]{3}/abc/'
text etc 123 more text
and also seeking to replace the entire string of numerics fails for me

Code:
echo "text etc 123 more text"|sed 's/[0-9]+/abc/'
text etc 123 more text


What am I not understanding please?

Last edited by keirvt; 06-10-2012 at 08:33 PM.
 
Old 06-10-2012, 08:51 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
You need sed -r

things like [...]{n} and + are part of the "extended" regular expressions. The sed -r flag turns that on.

OR--just "escape" the characters in question---eg: sed 's/[0-9]\{3\}/abc/' filename
 
1 members found this post helpful.
Old 06-10-2012, 09:33 PM   #3
keirvt
Member
 
Registered: Sep 2006
Location: Sydney Australia
Distribution: fedora/Ubuntu
Posts: 123

Original Poster
Rep: Reputation: 17
Great

Thank you for that, you'd think the experts out there would have mentioned that but perhaps I missed the fine print. anyway thanks for the tip.
 
Old 06-11-2012, 07:12 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,578

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
Quote:
Originally Posted by pixellany View Post
You need sed -r
Is there any reason to not use "-r" routinely, to avoid tripping on this same problem?

Daniel B. Martin
 
Old 06-11-2012, 07:22 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Quote:
Originally Posted by danielbmartin View Post
Is there any reason to not use "-r" routinely, to avoid tripping on this same problem?
-r is a GNU sed extension, and not available in other sed variants. For example, the SunOS 5.10 sed does not support -r. If you use systems without GNU tools, then you might wish to avoid relying on it.
 
2 members found this post helpful.
Old 06-11-2012, 07:26 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,883

Rep: Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191
I routinely use any and all extensions that are useful. Including this one. Where I run into situations where they don't apply, I worry about fixing it then.
Do it - but don't be too surprised it it doesn't always work.
 
1 members found this post helpful.
  


Reply


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
[SOLVED] sed command not matching? Eppo Programming 3 03-26-2012 12:11 PM
sed and regexp matching (GNU sed version 4.2.1) Ashkhan Programming 8 02-27-2012 09:12 AM
[SOLVED] Pattern matching with sed vinaytp Linux - Newbie 3 05-24-2010 07:33 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
sed matching an apostrophe dazdaz Linux - General 2 04-28-2006 05:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:39 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration