Lab ^

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Filter a file for lines that have exactly 5 numbers in a row.

  2. Relevant commands, code, scripts, algorithms:
    Use the grep command with any options and wild cards.

  3. The attempts at a solution (include all code and scripts):

 grep "[0-9]\{5\}" filterdata 
grep [0-9][0-9][0-9][0-9][0-9] filterdata
grep "[0-9]\{5,\}" filterdata
grep "[0-9]\{5\}" * filterdata
grep "[0-9]{5}" filterdata
grep "[0-9]\{5\}" . filterdata

(filterdata being the file Iam trying to Filter)

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Owens Community College, Toledo, Ohio, Thomas McLeary, EET 208

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

$ cat file
123456
12345
12345a dfdf
121
12345 dfd
$ grep -w "[0-9]\{5\}" file
12345
12345 dfd
1 Like

Check out the ^ and $ anchors in regular expressions. Also it says "numbers", not "digits" . -w may work but is not a portable grep option.

So if a line is like :

bla 12 34 890 56 78 bla bla

... it does have exactly 5 numbers in a row

Thanks for the aid. Tried the grep -w "[0-9]\{5\} file with no luck. the numbers are consecutive (12345) and I need to show the complete line that they are in.

The solution just depends on the way you understand the statement :

"Filter a file for lines that have exactly 5 numbers in a row"

1) do "numbers" stand for "digit" ?

2) what does "in a row" mean ? separated by a space ? other ?

3) does this means that the lines does have and only have 5 numbers in a row?

4) or may the line contain other non-numeric characters (may those "5 numbers in a row" be placed anywhere in the line) ?

this line has four 1234 numbers
this line has five 12345 numbers
this line has six 123456 numbers
this line has seven 1234567 numbers
this line has eight 12345678 numbers
part one of the lab is to just return the line with five numbers.

What's the business about "Without school/professor/course information, you will be banned if you post here!"?

The forum has agreed with education world in order to not keep on tracking posts ( though we do depending the type of request...) and refuse anything not from professionals to create a dedicated forum for schools/homework but it has strict conditions for both sides: The student agrees what he post here is seen by all AND can not remove once happy and has to comply to the rules by filling the template that has been accepted by education authorities... For the people helping, you are now NOT to answer the question/topic... but to help, guide or correct the student that has to do the work himself...

I am not sure what the problem is I have followed the templet provided. If examples of code connot be given to help solve a problem then all I need is a book not the expertise of the Forum. I have tried all the suggestion given after exhausting the information recieved in class. I do believe the problem is not with the code but with the sequence it is written in.

So what we have established is that what needs to be matched is 5 digits (not numbers) in a row and that to the left (from the beginning) and to the right (right until the end) of them there can be zero or more characters that are NOT numbers. The negative complement of a range or class of characters inside square brackets is done with the negation operator ^ as the first character inside those brackets.

-------
What we will do in this section is give hints and try to help you on your way to understanding, not provide complete answers. The reason for this is two-fold:

  1. We get letters from professors asking to please refrain from doing so.
  2. We would not actually be helping you. How would it help you if we provide that answer, you copy that answer, hand over your assignment and you will have learned absolutely nothing.

There is nothing easier for us than to just provide the answer, but then we really would not be helping you.

Retried your suggestion a second time it worked found that I fat fingered the command the first time

What about a line like this:

this line has five 12345 numbers and 1 extra number

Thanks for all the input. I reread all the threads and went back over what I had done and found that I had fat fingered one of the earlier commands that were suggested. The -w option made everything come together not only for that lab exercise but for two other exercises in that same lab.

1 Like