Thursday, November 23, 2006

grep command : Finds text within a file

The grep command searches for the pattern specified by the Pattern parameter and writes each matching line to standard output.
Examples

0. $ grep '12.00' /home/prabhat/backup/log.txt
This command basically shows how you can use grep to extract lines containing a particular string from a text file.
1. To use a pattern that contains some of the pattern-matching characters *, ^, ?, [, ], \(, \), \{, and \}, enter:
grep "^[a-zA-Z]" pgm.s
This displays every line in pgm.s whose first character is a letter.

2. To display all lines that do not match a pattern, enter:
grep -v "^#" pgm.s
This displays every line in pgm.s whose first character is not a # (pound sign).

3. To display all lines in the file1 file that match either the abc or xyz string, enter:
grep -E "abc|xyz" file1

4. To search for a $ (dollar sign) in the file named test2, enter:
grep \\$ test2
The \\ (double backslash) characters are necessary in order to force the shell to pass a \$ (single backslash, dollar sign) to the grep command. The \ (single backslash) character tells the grep command to treat the following character (in this example the $) as a literal character rather than an expression character. Use the fgrep command to avoid the necessity of using escape characters such as the backslash.

^ - match at the beginning of the line
$ - match at the end of the line

Some extra options for grep
-v
Reverses the normal behavior of the grep command - Instead of selecting lines, it rejects the lines that match the given criteria.
-c
It suppresses the normal output and only prints the total count of matching lines instead of the actual lines.
-i
Ignores the case of the text when matching the given pattern.
-w
Checks if the given pattern is a word by itself and not a part of another word. Thus if you search for 'bay' and the word 'baywatch' is present in a file, the particular line containing that word would not be returned in the result.
-l
Only gives the names of the files in which the given pattern was found.
-r
Checks for the given pattern , recursively within the directory that you specify after the -r option

know more
http://www.computerhope.com/unix/ugrep.htm
http://linux.about.com/od/commands/l/blcmdl1_grep.htm
http://www.lowfatlinux.com/linux-grep-manual.html

No comments:

SHOW ENGINE INNODB STATUS

  The SHOW ENGINE INNODB STATUS command in MySQL provides detailed information about the internal state of the InnoDB storage engine. This ...