Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I do not understand why this sed operation did not repeat "123":
$ echo "abc 123" | sed 's/[0-9]*/& &/'
abc 123
I was expecting output of:
abc 123 123
since 123 was matched,
and I have replacement for 123 as /& &/ = 123 123.
Or this
abc 123 abc 123
since "a" in abc is matches [0-9]*.
Obviously I misunderstood has regex matched numbers using pattern [0-9]*.
My understanding is that [0-9]* will match any set of digits starting from zero or more single digit like: 3, 00, 10, 234, a, ab, ...
My understanding is that [0-9]* will match any set of digits starting from zero or more single digit
When testing regex, use the -n switch on sed, and "p" to print your records. Then you know when you aren't doing what you want. (you're just getting the echo'd data here).
Also when using regex, use the -r switch.
That should get you started.
The problem as noted already is that the '*' matches zero or more digits at the beginning, so the output is 'nothing space nothing' (the zero digits that were matched, twice, separated by a space) followed by the rest of the string.
Use '+' instead...
Code:
echo "abc 123" |sed 's/[0-9]\+/& &/'
abc 123 123
Note the escaped '\+' which is not necessary with the -r option ('+' with -r).
Last edited by astrogeek; 02-05-2016 at 10:58 PM.
Reason: typos and changed wording for clarity
I went back and lookup this post while reading sed again.
Quote:
Originally Posted by astrogeek
The problem as noted already is that the '*' matches zero or
more digits at the beginning, so the output is
'nothing space nothing' (the zero digits that were matched,
twice, separated by a space) followed by the rest of the string.
Use '+' instead...
Code:
echo "abc 123" |sed 's/[0-9]\+/& &/'
abc 123 123
Note the escaped '\+' which is not necessary with the -r option ('+' with -r).
I finally got it.
The digits were matched and from man sed:
Code:
s/regexp/replacement/
Attempt to match regexp against the pattern space.
If successful, replace that portion matched with
replacement.
The replacement may contain the special character &
to refer to that portion of the pattern space which matched,
and the special escapes \1 through \9 to refer to the
corresponding matching sub-expressions in the regexp.
This is how I finally understood it.
Since & is 123 when a match is found,
then the effective replacement of found
match (123) in this case is /& &/ = /123 123/
This "123 abc" becomes "123 123 abc".
Thanks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.