This page was generated from 27.searches.mth.

This page is part of the course work for CS360: Systems Tools course offered by the Computer Science Department at CalState, San Bernardino, California, USA. It was generated by Dr. Dick Botting.

Contents


    Searching

    cs360/notes/27.searches

    Why

    You don't want to spend an hour looking for all the places in a program where you used the identifier 'true'. But you need to know where it was misused to fix the code.

    Sections


    (exam): 27.01, 27.02, 27.03, 27.04, 27.05, 27.06, 27.07, 27.08,27.16
    (useful): 27.01, 27.02, 27.03, 27.04, 27.05, 27.06, 27.07, 27.08,27.14,27.16, 27.17, 27.18, 27.19
    (Perhaps): 27.09
    (Csh): 27.15
    (Skip): 27.10, 27.11, 27.12, 27.13, 27.20

    See Also

    [ 26.patterns.html ] [ 26.patterns.html ]

    Exceptions

    27.04: Most greps don't have \< and \> in my experience. vi does.

    27.08: Most of our servers have agrep in /usr/local/bin and the source code in /usr/local/src/arizona or ~dick/src/arizona. I nice piece of work.

    27.15: A similar trick works in the Korn shell when your .profile defines EDIT and VISUAL and /bin/vi. You tap: ESC, k, A, and add the successive stages in the pipeline....

    We will cast Perl to the winds...

    Definitions

  1. grep::command, global regular expression print.
  2. egrep::command, extended grep.
  3. agrep::command. Arizona grep.
  4. fgrep::command, fixed string grep.

  5. options::= -any combination of option.
  6. option::= v | w | i | l | c ....

    Syntax

    [ Syntax in 26.patterns ] [ Syntax in 26.patterns ]

    Questions

      How would you find all the lines in a file "foo" containing digits?
    1. a. grep 0-9 foo ?
    2. b. grep [0-9] foo ?
    3. c. grep "0-9" foo ?
    4. d. grep "[0-9]" foo ?

      The option -v is used with the grep command to:

    5. a. use verbose output ?
    6. b. output all lines expect those containung the search pattern ?
    7. c. output all lines containing the search pattern and its line number ?
    8. d. use mutliple search patterns ?

      How do you search a file named 'foo' for all lines not beginning with a 'G'?

    9. a. grep -n ^G foo ?
    10. b. grep ^[^G] foo ?
    11. c. egrep -v [^G] foo ?
    12. d. none of the above ?

      Which of the following would be matched by the command,

        egrep -i '(foot|base)+ball' foo ?
    13. a. footBall ?
    14. b. FOOTbaseBall ?
    15. c. basebaseball ?
    16. d. All of the above ?

      How would you print out lines ending in e?

    17. a. grep '.*e$' filename. ?
    18. b. grep '^e' filename. ?
    19. c. grep '.*e' filename. ?
    20. d. grep 'e$' filename. ?

      What does 'grep -c' do?

    21. a. suppress error messages. ?
    22. b. all but matching lines are printed. ?
    23. c. count of matching lines is printed. ?
    24. d. search for expression as a word. ?

      How would you check to see if any of three different users were in the last twenty to log in?

    25. a. last -20 | grep user1 | grep user2 | grep user3 ?
    26. b. last -20 | egrep (user1|user2|user3) ?
    27. c. last -20 | grep 'user1|user2|user3' ?
    28. d. last -20 | egrep 'user1|user2|user3' ?

      How would you find all of the words in the dictionary which start with "un"?

    29. a. grep "un" /usr/dict/words ?
    30. b. grep "un*" /usr/dict/words ?
    31. c. grep 'un&' /usr/dict/words ?
    32. d. grep '^un' /usr/dict/words ?

      How do you find all lines in a file which indicate a time (e.g. 5:30)?

    33. a. grep '5:30' filename ?
    34. b. egrep [0-9][0-9][0-9] filename ?
    35. c. egrep '[0-9]:[0-9][0-9]' filename ?
    36. d. egrep '(0-9): (0-9)(0-9)' filename ?

      What egrep is used for usually ?

    37. a. searching for lines that match one of number of different pattern. ?
    38. b. looking for one of a list or file of words. ?
    39. c. for everyday searching. ?
    40. d. Faster searches for complex patterns in large files. ?

      What is the option used to tell grep to ignore case?

    41. a. -i ?
    42. b. -l ?
    43. c. -s ?
    44. d. -x ?

      Which command below would produce the same output as

       		grep '[lL]' ....
      except using egrep?
    45. a. egrep 'l or L' ... ?
    46. b. egrep l|L ... ?
    47. c. egrep 'l|L' ... ?
    48. d. egrep 'l||L' ... ?

      How do you find lines in a file which may match several different patterns?

    49. a. grep 'pattern1|pattern2|pattern3' file ?
    50. b. grep '[pattern1|pattern2|pattern3]' file ?
    51. c. egrep 'pattern1|pattern2|pattern3' file ?
    52. d. egrep '[pattern1|pattern2|pattern3]' file ?

    Submit Your Notes Here

    To earn credit for completing this part of the course you need to send me a short list of things you have learned. A simple way to do this is to follow this [click here [socket symbol] if you can fill this hole] link and fill in the form using copy and paste.

End