LinuxQuestions.org
Help answer threads with 0 replies.
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 12-13-2016, 07:53 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247

Rep: Reputation: 103Reputation: 103
understanding \{n,m\} or -E {n,m}


All right, so here's an example:
grep -E "1{2}" file.txt (red means match)
111111111000
00011
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?
 
Old 12-13-2016, 08:37 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,979

Rep: Reputation: 7888Reputation: 7888Reputation: 7888Reputation: 7888Reputation: 7888Reputation: 7888Reputation: 7888Reputation: 7888Reputation: 7888Reputation: 7888Reputation: 7888
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.
Old 12-13-2016, 08:39 AM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247

Original Poster
Rep: Reputation: 103Reputation: 103
Right, it makes perfect sense. Thanks.
 
Old 12-13-2016, 08:55 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,035

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
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.
Old 12-13-2016, 09:03 AM   #5
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247

Original Poster
Rep: Reputation: 103Reputation: 103
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?
 
Old 12-13-2016, 09:10 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,812

Rep: Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236
Quote:
Originally Posted by vincix View Post
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.
Old 12-13-2016, 09:14 AM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247

Original Poster
Rep: Reputation: 103Reputation: 103
Yeah, that was simple Thanks.

And if I wanted it to match all, then I'd do: grep -E "(Labs ?){3}"
 
Old 12-13-2016, 09:39 AM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,812

Rep: Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236
Quote:
Originally Posted by vincix View Post
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".
 
Old 12-13-2016, 09:44 AM   #9
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247

Original Poster
Rep: Reputation: 103Reputation: 103
The overall objective is simply understanding (extended) regex better, and go on to learn awk, sed better.
 
Old 12-13-2016, 09:45 AM   #10
BW-userx
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

Rep: Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243
Quote:
Originally Posted by grail View Post
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.
 
Old 12-13-2016, 09:47 AM   #11
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,247

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 12-13-2016, 09:56 AM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,812

Rep: Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236
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.
Old 12-13-2016, 09:59 AM   #13
BW-userx
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

Rep: Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243
Quote:
Originally Posted by vincix View Post
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.
 
Old 12-13-2016, 10:00 AM   #14
BW-userx
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

Rep: Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243Reputation: 2243
Quote:
Originally Posted by rknichols View Post
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.
 
Old 12-13-2016, 10:09 AM   #15
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,812

Rep: Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236Reputation: 2236
Quote:
Originally Posted by BW-userx View Post
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.
 
  


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
help understanding ln dr_zayus69 Linux - General 3 01-14-2005 08:33 PM
A better understanding! nny0000 Slackware 1 04-14-2004 12:01 PM
Understanding X?? ++ bdp Linux - General 2 02-25-2004 05:47 PM
Understanding X?? nny0000 Linux - General 2 02-25-2004 02:07 AM
Understanding df -k itsjustme Linux - General 6 10-28-2003 12:08 PM

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

All times are GMT -5. The time now is 05:53 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
Open Source Consulting | Domain Registration