Search code examples
regexgrepwildcardwc

Using grep and wc with wildcards on a file


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


Solution

  • 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/