Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-05-2016, 09:08 PM
|
#1
|
Member
Registered: Nov 2015
Posts: 397
Rep: 
|
sed - stream editor and regex [0-9]*
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, ...
What did I missed?
Thanks.
Last edited by fanoflq; 02-05-2016 at 10:08 PM.
|
|
|
02-05-2016, 09:40 PM
|
#2
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,444
|
Quote:
Originally Posted by fanoflq
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.
|
|
1 members found this post helpful.
|
02-05-2016, 09:40 PM
|
#3
|
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824
|
Not quite. [0-9]* matches zero or more 0-9's. Which matches first at the front of the string.
Code:
~$ echo "abc 123" | sed 's/[0-9]*/fjfg/'
fjfgabc 123
Try:
Code:
$ echo "abc 123" | sed 's/[0-9][0-9][0-9]/& &/'
abc 123 123
|
|
3 members found this post helpful.
|
02-05-2016, 10:04 PM
|
#4
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735
Rep:
|
Hi.
Code:
echo "abc 123" | sed 's/[0-9]\{1,\}/& &/'
abc 123 123
For systems like:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.3 (jessie)
sed (GNU sed) 4.2.2
Best wishes ... cheers, makyo
Last edited by makyo; 02-05-2016 at 10:07 PM.
|
|
1 members found this post helpful.
|
02-05-2016, 10:18 PM
|
#5
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Why do you want to use -n?
From man page:
-n, --quiet, --silent
suppress automatic printing of pattern space
$ echo "abc 123" | sed -n 's/[0-9]*/& &/p'
abc 123 <=== Is this the first pattern match?
$ echo "abc 123" | sed 's/[0-9]*/& &/p'
abc 123
abc 123
Why two different results?
|
|
|
02-05-2016, 10:36 PM
|
#6
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Pattern [0-9]* will be a valid match to anything.
So regex should return these:
a
ab
abc
abc ( there is a space after abc)
abc 1
abc 12
abc 123
b
bc
....
c
c + space
c 1
....
So I really should expect an infinite match.
This is where I not sure about correctness of sed's regex.
|
|
|
02-05-2016, 10:41 PM
|
#7
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,388
|
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
|
|
3 members found this post helpful.
|
03-03-2017, 04:59 PM
|
#8
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
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.
Last edited by fanoflq; 03-03-2017 at 05:02 PM.
|
|
|
All times are GMT -5. The time now is 04:29 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|