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.
|
|
08-23-2017, 05:03 AM
|
#1
|
LQ Newbie
Registered: Jul 2017
Posts: 26
Rep:
|
grep "Size: [0-9]\+ MB"
Hello everyone,
I study a command as follows:
Code:
dmidecode --type memory | grep "Size: [0-9]\+ MB"
I don't understand the meaning of " [0-9]\+ " .
What is the meaning of "\+" ?
Is it regular expression or ???
Thanks!
|
|
|
08-23-2017, 05:50 AM
|
#2
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
Anything in square brackets means list or range. [0-9] means any single integer 0 thru 9 inclusive. [12345] means any combination of one or more in the list. I think the "\+" might produce no output. But this exercise is so mundane I can only hope it's a class assignment and not actual on-the-job stuff.
You might want to change the quotes from double to single.
Last edited by AwesomeMachine; 08-23-2017 at 05:52 AM.
|
|
|
08-23-2017, 05:56 AM
|
#3
|
LQ Newbie
Registered: Jul 2017
Posts: 26
Original Poster
Rep:
|
I actually know what the [0-9] means.
I just want to know what is \+ doing.
|
|
|
08-23-2017, 06:33 AM
|
#4
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,984
|
\+ is one of these questionable BRE extensions in Linux.
Clear and portable is \{1,\} or + in egrep (or grep -E that uses ERE).
It means the preceding character(-wildcard) must exist 1 or more times.
Code:
dmidecode --type memory | grep "Size: [0-9]\{1,\} MB"
dmidecode --type memory | egrep "Size: [0-9]+ MB"
Last edited by MadeInGermany; 08-23-2017 at 07:20 AM.
|
|
1 members found this post helpful.
|
08-23-2017, 10:14 PM
|
#5
|
LQ Newbie
Registered: Jul 2017
Posts: 26
Original Poster
Rep:
|
Quote:
Originally Posted by MadeInGermany
\+ is one of these questionable BRE extensions in Linux.
Clear and portable is \{1,\} or + in egrep (or grep -E that uses ERE).
It means the preceding character(-wildcard) must exist 1 or more times.
Code:
dmidecode --type memory | grep "Size: [0-9]\{1,\} MB"
dmidecode --type memory | egrep "Size: [0-9]+ MB"
|
Thank you.
The explanation is so understandable for me.
|
|
|
08-23-2017, 10:22 PM
|
#6
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
Quote:
Originally Posted by MadeInGermany
\+ is one of these questionable BRE extensions in Linux.
Clear and portable is \{1,\} or + in egrep (or grep -E that uses ERE).
It means the preceding character(-wildcard) must exist 1 or more times.
Code:
dmidecode --type memory | grep "Size: [0-9]\{1,\} MB"
dmidecode --type memory | egrep "Size: [0-9]+ MB"
|
I knew that as well [0-9]+ matches one or more [0-9]. Just like an '*' matches zero or more.
But why the '\' in front of \+? That would indicate a literal '+', wouldn't it?
I use [0-9]+ and [0-9]{3} quite often, but I was looking forward to an answer to [0-9]\+
jlinkels
|
|
|
08-24-2017, 03:22 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,246
|
Quote:
Originally Posted by jlinkels
I was looking forward to an answer to [0-9]\+
jlinkels
|
You can always test:
Code:
root@host# dmidecode --type memory | egrep "Size: [0-9]\+ MB"
root@host# dmidecode --type memory | egrep "Size: [0-9]+ MB"
Size: 8192 MB
Size: 8192 MB
root@host# dmidecode --type memory | grep "Size: [0-9]\+ MB"
Size: 8192 MB
Size: 8192 MB
root@host# dmidecode --type memory | grep "Size: [0-9]+ MB"
root@host#
|
|
2 members found this post helpful.
|
08-24-2017, 06:52 AM
|
#8
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,984
|
In ERE (extended regular expression) the special meaning is {m,n} and +
In BRE (basic regular expression) the special meaning is \{m,n\}
Someone thought it makes sense to have \+ in BRE following the same logic. This is in newer Linux only; it is not in the standards.
Also some take-overs from PRE (Perl regular expression, highly compatible with ERE) went into newer Linux BRE and ERE, like \b \s \d I wouldn't use them: no standard, not portable.
Instead use
\< \> (left and right word boundary, quite portable in BRE but also no standard)
[[:space:]]
[0-9] or [[:digit:]]
|
|
2 members found this post helpful.
|
08-24-2017, 08:36 PM
|
#9
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
I knew there were some differences in syntax between BRE and ERE. I always thought ERE was an extension of BRE and upward compatible. Not so. That backslashes have an opposite effect in some situations in BRE and ERE I never knew. Never too old to learn.
jlinkels
|
|
1 members found this post helpful.
|
08-25-2017, 01:43 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,246
|
Quote:
Originally Posted by jlinkels
I knew there were some differences in syntax between BRE and ERE. I always thought ERE was an extension of BRE and upward compatible. Not so. That backslashes have an opposite effect in some situations in BRE and ERE I never knew. Never too old to learn.
jlinkels
|
Not so. Yes, you are right. And it is valid not only for grep, but for sed too.
|
|
|
08-25-2017, 03:02 AM
|
#11
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
I thought in BRE 1\+1=2 is literal 1+1=2, and 1+1=2 is 111=2 or 1111=2, and so on.
edit: never mind. I performed pan64's recommended test from #7.
Last edited by AwesomeMachine; 08-25-2017 at 03:05 AM.
|
|
|
All times are GMT -5. The time now is 08:27 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
|
|