Student needs grep command help

I am a Student in college struggling with Linux homework

This home work was created by my professor not out of the class text book and is frustrating me and the text book is a little frustrating as well

need help with 2 5 6
stuck on 2 currently so I know 5 and 6 will be even more frustrating for me

The contents of the smallFile is listed below.

  • Make a file on your system with that data. Assume each field is whitespace or tab separated. The format of the fields is below.
  • Do each of the 6 Displays below. Each will be a grep with regular expressions. Show the command that you used for each.
  • Do the Shell Script.

File fields format:
First_Name Last_name Degree GPA email_address Class_section
I added the " : " to try and help otherwise it was just white space

$ cat smallFile
John:Doe:ECE:3.54:doe@jd.home.org:111.222.3333
James:Davis:ECE:3.71:davis@jd.work.org:111.222.1111
Al:Davis:CS:2.63:davis@a.lakers.org:111.222.2222
Ahmad:Rashid:MBA:3.74:ahmad@mba.org:111.222.4444
Sam:Chu:ECE:3.68:chu@sam.ab.com:111.222.5555
Arun:Roy:SS:3.06:roy@ss.arts.edu:111.222.8888
Rick:Marsh:CS:2.34:marsh@a.b.org:111.222.6666
James:Adam:CS:2.77:jadam@a.b.org:111.222.7777
Art:Pohm:ECE:4.00:pohm@ap.a.org:111.222.9999
John:Clark:ECE:2.68:clark@xyz.ab.com:111.111.5555
Nabeel:Ali:EE:3.56:ali@ee.eng.edu:111.111.8888
Tom:Nelson:ECE:3.81:nelson@tn.abc.org:111.111.6666
Pat:King:SS:2.77:king@pk.xyz.org:111.111.7777
Jake:Zulu:CS:3.00:zulu@jz.sa.org:111.111.9999
John:Lee:EE:2.64:jlee@j.lee.com:111.111.2222
Sunil:Raj:ECE:3.36:raj@sr.cs.edu:111.111.3333
Charles:Right:EECS:3.31:right@cr.abc.edu:111.111.4444
Diane:Rover:ECE:3.87:rover@dr.xyz.edu:111.111.5555
Aziz:Inan:EECS:3.75:ainan@ai.abc.edu:111.111.1111
Lu:John:CS:3.06:lu.john@xyz.org:111.333.1111
Lee:Chow:EE:3.74:chow@lc.wcw.ord:111.333.2222
Adam:Giles:SS:2.54:giles@cric.org:111.333.3333
Andy:John:EECS:3.98:john@aj.ece.edu:111.333.4444
  1. Display lines, with line numbers, records of CS majors.
  2. Display lines, with line numbers, for students whose first name is John.
  3. Display lines, with line numbers, for students whose first or last name is Lee.
  4. Display lines for students whose e-mail addresses end with .org.
  5. Display lines for students with GPA above 3.69 but less than 4.0.
  6. Display lines for ECE majors with GPA 3.5 or above but less than 4.0.

Shell Script: Pick one of the grep commands that you created above. Put that grep command in a shell script. The name of the shell script is your choice. Run the shell script.
Turn in the contents of the shell script and the output of the shell script run.

attempt 1 and 2

grep -n '\<John\>' smallFile 

grep -n '\<John\>' smallFile | sort -t: +0 -1

out come

1:John:Doe:ECE:3.54:doe@jd.home.org:111.222.3333
10:John:Clark:ECE:2.68:clark@xyz.ab.com:111.111.5555
15:John:Lee:EE:2.64:jlee@j.lee.com:111.111.2222
20:Lu:John:CS:3.06:lu.john@xyz.org:111.333.1111
23:Andy:John:EECS:3.98:john@aj.ece.edu:111.333.4444

attempt 3

cat smallFile | grep John

out come

John:Doe:ECE:3.54:doe@jd.home.org:111.222.3333
John:Clark:ECE:2.68:clark@xyz.ab.com:111.111.5555
John:Lee:EE:2.64:jlee@j.lee.com:111.111.2222
Lu:John:CS:3.06:lu.john@xyz.org:111.333.1111
Andy:John:EECS:3.98:john@aj.ece.edu:111.333.4444

I need a out come of

1:John:Doe:ECE:3.54:doe@jd.home.org:111.222.3333
10:John:Clark:ECE:2.68:clark@xyz.ab.com:111.111.5555
15:John:Lee:EE:2.64:jlee@j.lee.com:111.111.2222

I am obviously not finding the information I need and need the help figuring it out

Waubonsee Community College
Sugar Grove Illinois
Professor: Tim Lippold
Linux/UNIX Operating System CIS180
Book being used is Harley Hahn's Guide to Unix and Linux

---------- Post updated at 08:24 PM ---------- Previous update was at 03:35 PM ----------

Update figured out grep command for 2

grep -n '^\<John\> smallFile

outcome

1:John:Doe:ECE:3.54:doe@jd.home.org:111.222.3333
10:John:Clark:ECE:2.68:clark@xyz.ab.com:111.111.5555
15:John:Lee:EE:2.64:jlee@j.lee.com:111.111.2222

now stuck on 5 and 6

Yes the ^ is the "beginning of the line" anchor.
Then you do not need the other \< "left word boundary" anchor.
But the \> "right word boundary" is useful so it does not match Johnny .
Even better would be the field separator, then it would not even match John-Mary . The field separator is the character : (or in the case of "space" a character set that consists of a character class [[:space:]] .
For your challenge "above 3.69 below 4.0" match a 3 then a dot then a character set 7-9 . Assuming that there is not a further decimal digit e.g. 3.699
Often forgotten: in a regular expression a plain . is an "any character" wildcard, so in order to match a literal dot you need to escape it \. or put it in a character set [.]

going to need example of the command to do that

I have given you some bricks with some explanations. You need to understand and assemble.
Please show your next attempt!

ok figure it out with

grep 3\.[7-9] smallFile

now I have to display lines for ECE majors with GPA 3.5 or above but less than 4.0
what would eliminate the numbers 2 and 4 before a dot

---------- Post updated at 03:42 AM ---------- Previous update was at 02:59 AM ----------

grep ECE 3\.[5-9] smallFile
grep 'ECE 3\.[5-9]' smallFile
grep ECE {3}\.[5-9] smallFile 
grep ECE '{3}\.[5-9]' smallFile

don't work to eliminate a 2or 4 before the dot

Look also at your book's description of basic regular expressions (BREs) for:

  • anchors ( ^ and $ ),
  • non-matching list bracket expressions ( [^list] ),
  • subexpressions ( \(BRE\) ),
  • repetition of expressions ( * ), and
  • interval expressions ( BRE\{min,max\} and BRE\{count\} ).

Can you combine the above concepts to produce a BRE that is anchored to the start of a line (matches zero or more occurrences of characters that are not your field delimiter followed by your field delimiter) a specific number of times to skip over the contents of a specific number of fields at the start of a line?

For example, what do you think the BRE in the following grep command will match?

grep -n '^\([^:]*:\)\{4\}[^:]*\.edu:' smallFile

Try running that command. Did it print the lines you expected it to print?
What do you think the BRE in the following grep command will match?

grep -n '1$' smallFile

What happens if you combine the above two commands in a pipeline?

grep -n '^\([^:]*:\)\{4\}[^:]*\.edu:' smallFile | grep '1$'

What happens if you use a single grep to search for both BREs?

grep -n -e '^\([^:]*:\)\{4\}[^:]*\.edu:' -e '1$' smallFile

Does one of the above two suggestions provide a logical OR of the two BREs? Does one of the above two suggestions provide a logical AND of the two BREs?

this worked

grep 3\.[5-9] smallFile | grep ECE

thanks for all the help it is much appreciated

now that I have been up about 20 trying to figure this out I can get some sleep now that I figure the 6 out next is shell script to be done when I wake up

There's a fundamental difference between form 1, 3 and 4 and form 2 of your commands. Can you tell it?
Why shouldn't one form (which one?) work given you match the field separators in data file and regex?

Put the grep expression in " " or ' ' quotes, otherwise even the shell tries substitutions.
Use bounds, at least "word boundary" anchors, better the field separator.

Does that work if you use a BRE similar to that to look for a GPA greater than or equal to 3.00 and less than 3.50?

P.S.: How do you know the two digits and the decimal point you are matching are in the GPA field and not in the last field of your data?