LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-23-2017, 05:03 AM   #1
iorih0304
LQ Newbie
 
Registered: Jul 2017
Posts: 26

Rep: Reputation: Disabled
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!
 
Old 08-23-2017, 05:50 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,511

Rep: Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006
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.
 
Old 08-23-2017, 05:56 AM   #3
iorih0304
LQ Newbie
 
Registered: Jul 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
I actually know what the [0-9] means.
I just want to know what is \+ doing.
 
Old 08-23-2017, 06:33 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,267

Rep: Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580
\+ 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.
Old 08-23-2017, 10:14 PM   #5
iorih0304
LQ Newbie
 
Registered: Jul 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by MadeInGermany View Post
\+ 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.
 
Old 08-23-2017, 10:22 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,191

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
Quote:
Originally Posted by MadeInGermany View Post
\+ 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
 
Old 08-24-2017, 03:22 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,500

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
Quote:
Originally Posted by jlinkels View Post
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.
Old 08-24-2017, 06:52 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,267

Rep: Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580
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.
Old 08-24-2017, 08:36 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,191

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
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.
Old 08-25-2017, 01:43 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,500

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
Quote:
Originally Posted by jlinkels View Post
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.
 
Old 08-25-2017, 03:02 AM   #11
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,511

Rep: Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006
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.
 
  


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 07:58 AM
Usage of "-f" option of grep in a script results in "Permission Denied" error zaayu87 Linux - Newbie 3 07-25-2013 01:45 PM
What are the options "Nosuid" "mode" "size" in /etc/fstab? tuxfiles.org does not help pstein Linux - Newbie 1 11-16-2012 01:58 AM
Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing adamlucansky Linux - General 8 09-25-2009 08:26 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:33 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration