Week 7 Metacharacters for grep(1), egrep(1): . (dot) [] square brackets \{\} \<\> \ ^ $ * ? + \(\) | grep [options] regexp [files] Search one or more files for lines that match a regular expression regexp. Regular expressions are described in Section 6. Exit status is 0 if any lines match, 1 if not, and 2 for errors. See also egrep and fgrep. Options -b Precede each line with its block number. -c Print only a count of matched lines. -h Print matched lines but not filenames (inverse of -l). -i Ignore uppercase and lowercase distinctions. -l List filenames but not matched lines. -n Print lines and their line numbers. -s Suppress error messages for nonexistent or unreadable files. -s Silent mode: print only error messages, and return the exit status. -v Print all lines that don't match regexp. -w Restrict regexp to matching a whole word (like using < and >> in vi). ------------------------------------------------------------------------ To search a file using regular expression: grep -n '[dD]on\'t' tasks This uses a regular expression to find and display each line in the file tasks that contains the pattern don't or Don't. The line number for each line is also displayed. The expression is quoted to prevent the shell expanding the metacharacters [, ] and '. Double quotes are used to quote the single quote in dDon't. ------------------------------------------------------------------------ To use the output of another command as input to the grep command: ls -l | grep '^d........x' This lists all the directories in the current directory for which other users have execute permission. The expression is quoted to prevent the shell interpreting the ^ metacharacter. ------------------------------------------------------------------------ To redirect the results of a search to a file: grep Smith /etc/passwd > smurffs This searches the passwd file for each occurrence of the name Smith and places the results of this search in the file smurffs. There being a lot of Smiths everywhere this is quite a large file. ------------------------------------------------------------------------ Finding all uses of a word To find all uses of the word "Posix" (in any case) in the file text.mm, and write with line numbers: % /usr/bin/grep -i -n posix text.mm ------------------------------------------------------------------------ Finding all empty lines To find all empty lines in the standard input: % /usr/bin/grep ^$ or /usr/bin/grep -v . ------------------------------------------------------------------------ % /usr/bin/grep -E 'abc def' % /usr/bin/grep -F 'abc def' ------------------------------------------------------------------------ Sample searches for practice. Based on a large text file provide REs for grep/egrep use. a. Search for lines that have leading white spaces b. Search for lines that have trailing blanks c. Search for lines that begin with blanks followed by 0 or more digits, then a blank. Refer to http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/cmp/cmp.c for sample text, that has this format. [ when you cut and paste you will notice this ] d. Search for lines that have atleast one digit e. Search for lines that do not have any digit characters f. Search for lines that have one trailing blank g. Search for lines that have atleast three consecutive occurrences of "abc" in a line h. Search for lines that begin with "#include" i. Search of lines that contain relational operators of C language. Shell topics: Shell quoting characters. "", '',``, and backslash and examples. CIS$ echo "Unix version is " `uname -a`" CIS$ echo "Unix os release is \" Linux\" " Single quotes quotes all, does not allow variable name substitution, file name substitution Double quotes are less restrictive, allows variable name substitution. but does not allow filename substitution. Embedded quotes.