Basic use of pipe and grep command

Pipe and grep is a very powerful tool of the Linux command line. So before we start the basic use of the pipe and grep command we need to understand what is pipe and grep and how to use both tools.

What is a Pipe

The pipe is a way by which we can use two or more commands simultaneously. Where the output of one command works as an input of another command. We denote with the | symbol. We can create a lot of powerful commands with deadly combinations with other commands. Let us understand this with an example.

When we use the “cat” command then we can view the file and the wc command will count the number of lines, number of words, and number of bytes.

touch fruits_list # Creating file
cat > fruits_list # Concatenate
#CTRL-D to save and exit
cat fruits_list | wc
       4       4      29

The ‘grep’ command


grep [options] pattern [files]

A grep is one of the most important commands. You should master it. Suppose that we want to search for particular information from the text file. Suppose that we have created a file in which we have a list of fruits and we want to search for a particular fruit. Let us understand this with an example.

cat fruits_list | grep apple

We can also use case-insensitive search using the -i option, and we can count the number of matches using -c.

cat fruits_list | grep -i apple
cat fruits_list | grep -c apple

Some basic example of grep

show lines containing xyz in myFile

grep 'apple' fruits_list

Show lines containing msg in all files ending in HTML in current dir top-level files

cat > index.html
my msg to you.    
grep 'msg' *html
my msg to you.

Also Read: Install and remove linux packages, Basic use of ls or list command

About the Author: Pankaj Bisht