grep command in Linux

“grep” stands for “Global Regular Expressions Print”.  The grep filter scans a file for a certain sequence of characters and shows all lines containing that sequence. Regular expression refers to the pattern that is searched inside the file.

Command

$ grep [options] pattern [file]

Options

Here are details about some options.

OptionDetails
-iIgnore case differences in patterns and input data so that characters that only differ in case will match.
-fObtain patterns from File, one per line.
-eThis option allows you to provide an expression. Can use numerous times.
-hDisplay the matched lines, but do not display the filenames.
-cPrint out the number of matching lines in each file.
-lOnly shows a list of filenames.
-nShow the lines that match and their line numbers.
-vThis makes all the lines that do not fit the pattern print out.
-wSelect  only  those  lines  containing  matches  that form whole words.
-oOnly print the parts of a line that match, with each part on a separate line.
-RRead  all  files  under each directory, recursively.

Examples

  • Now, let us see some examples.
grep -i linux linux_introduction.txt
grep -c Linux linux_introduction.txt
grep -w Linux linux_introduction.txt
grep -n Linux linux_introduction.txt
grep -l Linux *
grep -iR Ubuntu .

Feedback

Do you want to provide us with feedback? Provide us with feedback directly at info@elearnbee.com.

Share us:

AD BLOCK 1