Using grep to locate files

If you give grep two or more files to search, it it includes the name of the file before each line of output. The following command searches for lines cintaining the string “JAVA” in all of the files in the current directory.

  • $grep JAVA *

schools : 100 vinod java k2schools

k2schools: 110 rajesh java k2schools

searching for patterns using regular expressions:

So for we have used grep to search for specific words or strings of text, but grep also allows you to search for targets defined as patterns that may match a number of different words of strings. You specify patterns for grep using the same kinds of regular expressions that were described in the discussion of text editing,

In fact “grep” stands for “global regular expression and print“. The rules and symbols used for forming regular expression for ed and vi can also be used with grep to search for patterns.

  • $grep ‘ch.*se’ recipes

will find entries containing “chinese” or “cheese”. The dot(.) matches any number of character except newline(any number include zero), the asterisk specifies any number of repetetions; together they indicate any string of any characters. (this pattern matches any string begining with “ch” and ending with “se” )

Note that in this example the target pattern “ch.*se” is enclosed in single quatation marks. This prevents the asterisk from being treated by the shell as a filename wildcard. In general, remember to put in quotes any regular expression that contains as asterisk or any other character that as special meaning for the shell.

The grep command useful options:

A large number of options are available that let your modify the way grep works. Especially three usefull options -v, -i, and -l, let you find lines that do not match the target, ignore uppercase and lowercase distinctions, and list only the line numbers on which your target can be found.

  • $grep -v schools k2schools

it displays all line in k2schools files that do not contain schools.

  • $file * | grep -v text

it displays all files in the current directory that are not text files by piping the output of file to grep -v

 

The grep command useful options:

A large number of options are available that let your modify the way grep works. Especially three usefull options -v, -i, and -l, let you find lines that do not match the target, ignore uppercase and lowercase distinctions, and list only the line numbers on which your target can be found.

  • $grep -v schools k2schools

it displays all line in k2schools files that do not contain schools.

  • $file * | grep -v text

it displays all files in the current directory that are not text files by piping the output of file to grep -v

CheapSexCams
You can leave a response, or trackback from your own site.

Leave a Reply

Powered by k2schools
%d bloggers like this: