Artikel Terbaru

Monday, 20 May 2013

Best 10 Simple examples of Grep Command in linux and Unix with tutorials

Top 10 Simple examples of Grep Command with explaination in linux
Grep is popular and powerful command in linux and unix operating system. Each developer and QA in software development need to know about grep command usage.
Questions on Grep is asked by almost all interviews for developer/QA role. Every one in Software field must know the Grep Usage and commands
Grep is used to search in file or multiple files for matching keyword or lines. Grep search for a word and return the line in set of one or more files.Grep command can be input to some other command and Grep can be used in variety of ways as follows

Grep Command usage syntax:-
grep 'word' /file/name or /directory/path
Below is the sample file for all grep examples.
$ cat cloud.txt
This tutorial is about grep command in linux and unix usage
Grep is powerful search tool for searching words in set of files
Grep can be used with other linux commands
Grep is so fast compared to other commands in linux systems..

How to search for a word in a file

$ $ grep 'word' cloud.txt
Grep is powerful search tool for searching words in set of files

Keyword ‘word’ searched in in cloud.txt return the matching line which has ‘word’.
By default grep without any options search for files if file name is specified, and directories, not sub directories if directory specified

How to do case insensitive search for a word in a files.

By default Grep command in linux or unix treats each case sensitive word as different.
To do case insensitive search, Grep command has option -i

$ grep -i "Grep" cloud.txt
It print all lines in cloud.txt file as grep variations (Grep, grep) is presented in each line of file.

Count of words matched in a file

$ grep -c "Grep" cloud.txt
And output is 3, if we add option –I , output is 4.

Recursive search using grep command

By default Grep command search files in current directories, To do sub directories , have to use -r option

$ grep -r "Grep"  /usr/cloud

Please click +1 if you like this post.