Grep is a 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 a file or multiple files for matching keyword or lines. Grep search for a word and return the line in the set of one or more files. Grep command can be input to some other command and Grep can be used in a 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 a powerful search tool for searching words in the 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 a powerful search tool for searching words in the set of files
Keyword ‘word’ searched in cloud.txt return the matching line which has ‘word’. By default grep without any options search for files if the file name is specified, and directories, not subdirectories if directory specified
How to do case insensitive search for a word in a file.
By default Grep command in Linux or Unix treats each case sensitive word as different. To do a case-insensitive search, Grep command has the option -i
$ grep -i “Grep” cloud.txt It prints all lines in cloud.txt file as grep variations (Grep, grep) is presented in each line of the file.
Count of words matched in a file $ grep -c “Grep” cloud.txt And the output is 3 if we add option – I, the output is 4.
Recursive search using grep command
By default Grep command search files in current directories, To do subdirectories, have to use -r option
$ grep -r “Grep” /usr/cloud
Please click +1 if you like this post.