LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 12-26-2010, 07:13 AM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,347

Rep: Reputation: 135Reputation: 135
sed doubt


I have a simple doubt in sed:

PHP Code:
#echo "abc 123" | sed 's/[0-9][0-9]*/& &/'

abc 123 123 
but

PHP Code:
#echo "abc 123" | sed 's/[0-9]*/& &/'

abc 123 
why not two 123 ?
 
Old 12-26-2010, 07:23 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
The asterisk means zero or more occurrences of the previous expression, so that it can match the null string. In this case it matches the null string at the beginning of the line (since there are no digits there) and substitutes it with itself (nothing), plus a blank space (that one between the two ampersand in the substitution string):
Code:
$ echo "abc 123" | sed 's/[0-9]*/& &/'
 abc 123
^ note the blank space here!
You can easily see this behaviour if you add more ampersands:
Code:
$ echo "abc 123" | sed 's/[0-9]*/& & & & &/'
    abc 123
The actual string is left untouched but a sequence of (matched) null strings and blank spaces is added at the beginning.

Last edited by colucix; 12-26-2010 at 07:34 AM. Reason: fixed erroneous explanation
 
1 members found this post helpful.
Old 12-26-2010, 07:39 AM   #3
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
The construct '[0-9]*' means zero or more numbers. Zero numbers is the null string. The first place a null string occurs is before the letter 'a'. This clarifies:

Code:
$ echo "abc 123" | sed 's/[0-9]*/321/'
321abc 123
What you actually want is one or more numbers, provided by the regular expression:

Code:
$ echo "abc 123" | sed -r 's/[0-9]+/& &/'
abc 123 123
 
1 members found this post helpful.
Old 12-26-2010, 07:59 AM   #4
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,347

Original Poster
Rep: Reputation: 135Reputation: 135
Thanks a lot, I had the misunderstanding that in [0-9][0-9]* , it matches 0 or more of number.

but it matches first one digit from first [0-9] then 0 or more from 2nd [0-9]. So that it should match a number.
 
  


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
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
One doubt please .. vibinlakshman Slackware 1 02-18-2009 07:57 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
Doubt in C++ zeropash Programming 1 07-05-2005 12:33 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 06:18 AM.

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