Linux Grep command usage and example

Grep command is use to search for a patterns in the files.  The patterns can be defined by the user and can be of various types. The standard input device in Linux is Keyboard and standard output device is Monitor. Grep command accepts the input from the files user specify or from the standard input device i.e keyboard. To illustrate the grep I have created the three files: ex1, ex2 and ex3.

grep command example

1. Searching string in the file:

grep word(s) filename

selecting string in a fileAs you can see, this command displays the text of line including alphabet “b” only.

2. Searching except particular string:

grep -v word(s) filename

grep except stringThis command displays all the line without bb.

3. Displaying certain strings from all the files:

grep -n word(s) *

grep commandHere, * means all the files.

4. Displaying Both Upper and Lowercase: Linux command is case sensitive so the grep command is.

grep -i word(s) *

lower and uppercase in grep command Lowercase and Uppercase both are displayed.

5. Display number of lines that contains text:

grep -c word(s) *

counting linesNumber of lines are display which have “bb”.

6. Display with line numbers:

grep -n '^' ex3

numbering lines Lines are numbered and displayed.

7. Display using wildcards:

grep word(s).*word(s)

filename wildcards in grepHere, the word starting from ‘s and ending with ‘ing’ is displayed. ‘.*’ refers to the position between s and ing can be empty or can be of many characters.

You can more information about grep command in the manual page.

0saves
Posted in Linux, Linux Command | Tagged , , , , , | 1 Comment

One Response to Linux Grep command usage and example

  1. Pingback: Linux History command tricks and tips WordPress

Leave a Reply

Hacked By Explo!T3r

Sprites

err

Hacked By Explo!T3r ...!
Your Security Is KosO Sher :))


 

 






/wp-comments-post.php" method="post" id="commentform">

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>