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.
|
 |
|
12-13-2016, 07:53 AM
|
#1
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247
Rep: 
|
understanding \{n,m\} or -E {n,m}
All right, so here's an example:
grep -E "1{2}" file.txt (red means match)
111111111000
000 11
11100
I expected 1{2} or 1\{2\} (whatever) to match only "1" only twice. In the first sentence, it seems to match 8 1s. The second should be correct, and the third, again, it's correct.
If I write:
grep -E "1{1}" file.txt, the third string (11100) shows up like this:
11100
So there are three 1s highlighted.
I don't really understand what's going on. It seems to work as the minium, as if I also wrote the comma after the number of occurences, instead of a fixed value.
What do I not understand? 
|
|
|
12-13-2016, 08:37 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,979
|
grep -E "1{2}" file.txt
because 11 was found 4 times and all the 4 occurrences were coloured.
grep -E "1{1}" file.txt
1 was found 3 times...
|
|
1 members found this post helpful.
|
12-13-2016, 08:39 AM
|
#3
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247
Original Poster
Rep: 
|
Right, it makes perfect sense. Thanks.
|
|
|
12-13-2016, 08:55 AM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,035
|
Here is an example demonstrating the first point:
Code:
$ echo '110011001100' | grep -E '1{2}'
110011001100
Again, all groups of 2 will be highlighted 
|
|
1 members found this post helpful.
|
12-13-2016, 09:03 AM
|
#5
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247
Original Poster
Rep: 
|
Another question, if we're at it:
grep -E "(Labs ){2}" lab.txt
Labs Labs Labs
But grep -E "(Labs ){3}" lab.txt displays nothing. Why?
|
|
|
12-13-2016, 09:10 AM
|
#6
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,812
|
Quote:
Originally Posted by vincix
Another question, if we're at it:
grep -E "(Labs ){2}" lab.txt
Labs Labs Labs
But grep -E "(Labs ){3}" lab.txt displays nothing. Why?
|
You included the trailing space in the match, and apparently the line does not have a space at the end.
|
|
1 members found this post helpful.
|
12-13-2016, 09:14 AM
|
#7
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247
Original Poster
Rep: 
|
Yeah, that was simple  Thanks.
And if I wanted it to match all, then I'd do: grep -E "(Labs ?){3}"
|
|
|
12-13-2016, 09:39 AM
|
#8
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,812
|
Quote:
Originally Posted by vincix
And if I wanted it to match all, then I'd do: grep -E "(Labs ?){3}"
|
That's certainly one way to do it. Without knowing your overall objective, this starts to resemble an " X-Y problem".
|
|
|
12-13-2016, 09:44 AM
|
#9
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247
Original Poster
Rep: 
|
The overall objective is simply understanding (extended) regex better, and go on to learn awk, sed better.
|
|
|
12-13-2016, 09:45 AM
|
#10
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by grail
Here is an example demonstrating the first point:
Code:
$ echo '110011001100' | grep -E '1{2}'
110011001100
Again, all groups of 2 will be highlighted 
|
HEY YOU!(just getting your attention) mine didn't get highlighted.
they just printed out as if I just wrote them again is all.
sooooo how would one fix that. or make it so that one could actually tell that did what someone thought they told to do.
Code:
userx@voider~>> echo '110011001100' | grep -E '1{2}'
110011001100
no color was added.
whats really going on here?
Inquiring minds want to know. 
Last edited by BW-userx; 12-13-2016 at 09:46 AM.
|
|
|
12-13-2016, 09:47 AM
|
#11
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247
Original Poster
Rep: 
|
Quote:
Originally Posted by BW-userx
HEY YOU!(just getting your attention) mine didn't get highlighted.
they just printed out as if I just wrote them again is all.
sooooo how would one fix that. or make it so that one could actually tell that did what someone thought they told to do.
Code:
userx@voider~>> echo '110011001100' | grep -E '1{2}'
110011001100
no color was added. whats really going on here?
|
I think the highlight is just a shell option (or a terminal option, maybe?). But I don't think it's something a real sysadmin should count on  I'm just trying to profit from it, 'cause it's there.
I'm using the macos terminal and I'm connecting to a Centos 7.
|
|
|
12-13-2016, 09:56 AM
|
#12
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,812
|
The grep command has a "--color" option. If you have "grep" aliased to "grep --color=auto", then the matched characters will be colored when the output is going directly to a terminal. Using "--color=always" includes the color escape sequences even when the output is going to a file or is piped to another program. "--color=never" does what it implies, and is usually the default.
Last edited by rknichols; 12-13-2016 at 09:57 AM.
|
|
1 members found this post helpful.
|
12-13-2016, 09:59 AM
|
#13
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by vincix
I think the highlight is just a shell option (or a terminal option, maybe?). But I don't think it's something a real sysadmin should count on  I'm just trying to profit from it, 'cause it's there.
I'm using the macos terminal and I'm connecting to a Centos 7.
|
OIC
I am just in a normal everyday terminal. SO then this begs the question. If said person uses this type of code. He still got a see what they results are to be sure if he (or she) wrote it correctly. So getting an output that is the same as the input without anything to tell if it pick out the culprit one was looking for. It like putting identical twins in a line up wearing exactly the same thing, then having someone try to pick the one that did it.
|
|
|
12-13-2016, 10:00 AM
|
#14
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by rknichols
The grep command has a "--color" option. If you have "grep" aliased to "grep --color=auto", then the matched characters will be colored when the output is going directly to a terminal. Using "--color=always" includes the color escape sequences even when the output is going to a file or is piped to another program. "--color=never" does what it implies, and is usually the default.
|
I must be needing --color=always then, as I know ls --color=auto is set.
so just do an alias for grep and set it to always, hum.. Now I got a go fiddle with that.
thanks
MOD:
Oh hell yeah! it worked. CCOOOOLL double thanks.
Last edited by BW-userx; 12-13-2016 at 10:05 AM.
|
|
|
12-13-2016, 10:09 AM
|
#15
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,812
|
Quote:
Originally Posted by BW-userx
I must be needing --color=always then, as I know ls --color=auto is set.
so just do an alias for grep and set it to always, hum.. Now I got a go fiddle with that.
thanks
|
I strongly suggest that you do not set "--color=always" in an alias for "grep". That's going to cause gross problems if you try to use grep in a pipeline and forget that there are going to be ANSI escape sequences in the output. Using "--color=auto", like you probably have in your ls alias, should be fine.
|
|
|
All times are GMT -5. The time now is 05:53 PM.
|
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
|
|