List

The grep Command

The grep command searches a file or files for lines that have a certain pattern.

Syntax

$grep pattern file(s)

Options

 

Sr.No. Option & Description
1 -v

 

Prints all lines that do not match pattern.

2 -n

 

Prints the matched line and its line number.

3 -l

 

Prints only the names of files with matching lines (letter “l”)

4 -c

 

Prints only the count of matching lines.

5 -i

 

Matches either upper or lowercase.

Example

[nishant@localhost ~]$ ls -l
total 104
-rw-r–r–. 1 root root 44013 Aug 31 13:40 cwp2-latest
drwxr-xr-x. 3 nishant nishant 4096 Oct 14 2019 Desktop
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Documents
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Downloads
-rw-r–r–. 1 root root 14540 Nov 5 2012 epel-release-6-8.noarch.rpm
-rw-rw-r–. 1 nishant nishant 413 Aug 29 2016 file
-rwxrwxr-x. 1 vikas nishant 2033 Aug 29 2016 file1
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Music
-rw-rw-r–. 1 nishant nishant 57 Aug 29 2016 newfile
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Pictures
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Public
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Templates
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Videos

In the above result, it is clear that there are files created in August, January, October but if need to search files from January only, then we can use “grep” command along with the “pipes” command like as below.

[nishant@localhost ~]$ ls -l | grep “Jan”
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Documents
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Downloads
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Music
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Pictures
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Public
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Templates
drwxr-xr-x. 2 nishant nishant 4096 Jan 20 2015 Videos

grep Search from file

[nishant@localhost question]$ grep Musa banana.txt
Banana (Musa species) is an important fruit of tropics.
It may be one of the reasons why the banana is called Apple of Paradise and botanically named Musa paradisiaca.

In the above example the word “Musa” was search from the file “banana.txt” and the word has been found in two lines in the file, so the grep command displays only those two line from the file.

Leave a Reply

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

  Posts

1 2 3
October 20th, 2020

Compute the Compound Interest.py

Write a function with name compound_interest that takes three arguments: principle, rate and years in order. the rate is float […]

October 18th, 2020

String Data Type

A string is a sequence of characters. String Data Type str1=”hello” print(type(str1)) Ans: <class ‘str’> str2=’123′ print(type(str2)) Ans: <class ‘str’> […]

October 18th, 2020

String Library

String Replace str=”Hello Bob” print(str) rstr=str.replace(‘Bob’, ‘James’) print(rstr)   This will replace the Bob with James and store it in […]

October 17th, 2020

‘in’ statement in String

  fruits=’banana’ bana in fruits Ans: True Python Function to confirm he vowel in the given input. def is_vowel(l): return […]

September 24th, 2020

awk Marking keywords

Marking keywords In the following example, we mark Java keywords in a source file. $wget nishantmunjal.com/dataset/mark_keywords.awk # the program adds […]

September 24th, 2020

awk Rock-paper-scissors

Rock-paper-scissors Rock-paper-scissors is a popular hand game in which each player simultaneously forms one of three shapes with an outstretched […]

September 24th, 2020

awk Spell Checking

Spell checking We create an AWK program for spell checking. $wget nishantmunjal.com/dataset/spellcheck.awk BEGIN { count = 0 i = 0 […]

September 24th, 2020

awk Pipes

Pipes AWK can receive input and send output to other commands via the pipe. $ echo -e “1 2 3 […]

September 24th, 2020

awk Passing variables

Passing variables to AWK AWK has the -v option which is used to assign values to variables. For the next program, we […]

September 24th, 2020

awk more

Passing variables to AWK Pipes-awk awk Spell Checking Rock Paper Scissor Marking Keywords