-1

Display:

1) number of students whose last name is Yang.

My work:

1) grep "[Yang] [A-Za-z]* [A-D][+]* [A-Za-z]*" students.txt | wc -l

Comment: Regex seems ok but outputs 6 instead of 3

geforce
  • 61
  • 6
  • One question per question, please; there will be duplicates for all four. But briefly, (1) `grep -iw`; (2) `grep -c`; (3) + (4) also, properly quote your regex. – tripleee Nov 21 '15 at 20:39
  • shortened to 1 question – geforce Nov 21 '15 at 20:55
  • 1
    asking one question and getting 4 answers - You must be in luck ;-) – user2692263 Nov 21 '15 at 21:00
  • I actually just solved them all except 4) ..... my answer for that one is "grep "[^Yang] [A-Za-z]* [A-D][+]* [A-Za-z]*" students.txt | wc -l" it outputs 4 instead of 3 any idea why? – geforce Nov 21 '15 at 21:03
  • 1
    Yup - the hat inside the brackets means something different - try the grep without the word count - and try tme out at regexr.com – user2692263 Nov 21 '15 at 21:08

1 Answers1

1

1) add a space inside pattern, so Johnson is hidden grep -i "John " students.txt

2) Wildcards can never be better than exact value - stick with it

3) look at 1 - add a space, so it is a wildcard like " A \| A+ " i escaped the pipe sign, because i use double quotes and not single quotes (i believe)

4) Yang is lastname, so it is first - that You can check for with the hat on: '^Yang'

regular expressions are fun - and sometimes time consuming. There are good places on the net - try http://regexr.com/

user2692263
  • 475
  • 4
  • 8