LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed doubt (https://www.linuxquestions.org/questions/programming-9/sed-doubt-852438/)

divyashree 12-26-2010 07:13 AM

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 ?

colucix 12-26-2010 07:23 AM

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.

macemoneta 12-26-2010 07:39 AM

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


divyashree 12-26-2010 07:59 AM

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.


All times are GMT -5. The time now is 02:54 PM.