grep, egrep, and regular / extended regular expressions [Help!]

Status
Not open for further replies.

iFargle

Linux / HPC / K8s SME
Staff member
Messages
4,403
Location
N/A
I am lost as to how to do this.. I understand basic regular expression syntax, but I can't seem to wrap my head around this one.

This is a homework assignment.. Just to clarify. I've looked everywhere but can't find an answer :tongue:

The question:

Using grep -E on the above file, display the number of students who received a lab grade of 5 six times consecutively.

The file:

0673 4 5 5 5 5 5 5 5 5 4 67 80 83
0788 5 5 5 5 5 5 5 5 5 5 100 87 105
0821 5 5 5 5 5 5 5 5 5 5 93 80 88
1641 5 5 5 4 5 5 5 0 5 4 87 47 94

My answer:

So far really none... This is what I think it should be, but I'm obviously wrong :tongue:
grep -E '(5\s)\{6,\}' file
I can get it to display consecutive fives with grep, not egrep (and only fives, not the spaces before or after it):
grep '5\{6,\}' file
But now I'm stuck. I know with extended regular expressions the parentheses group patterns together and the \{\} indicates how many times a character occurs before it is listed, but I can't seem to find what I need to: a 5 and a space consecutively.

Unless I'm going about this in a totally wrong direction...
let me know if that's the case :tongue:

Thanks for any help :thumbsup:
 
grep -E '(5\s)\{6,}\' file


You are so close with this expression. there's only 3 unneeded characters. 2 are the same.
Remember, {} is a sequence, so you should only need {6}.
That's all I can say w/o giving you the answer. (Although, some could argue I already have).
 
omg I just realized what I was doing wrong.. It isn't a single space, it's two spaces. :anger: :cry: :tongue:
Also.. I thought that was only with certain commands. Glad to know that works with grep :tongue:

Thank you for your help :thumbsup:

For those wondering.. my final answer:

grep -E '(5 ){6,}' file
If you can't see the second space...
grep -E '(5<space><space>){6,}' file
 
Status
Not open for further replies.
Back
Top Bottom