Pipe and grep is a very powerful tool of the Linux command line. So before we start Basic use of 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 | symbol. We can create a lot of powerful commands with deadly combination with other commands. Let us understand this with an example.
When we use “cat” command then we can view file and wc command will count the number of lines, number of words, number of bytes.
touch fruits_list cat > fruits_list apple banana grape blueberry #CTRL-D to save and exit cat fruits_list | wc
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 apple,
We can also use case insensitive search using -i option, we can count of the number of matches using -c.
cat fruits_list | grep -i apple # apple, cat fruits_list | grep -c apple # 1
Some basic example of grep
show lines containing xyz in myFile
grep 'apple' fruits_list apple,
Show lines containing msg in all files ending in html in current dir top level files
grep 'msg' *html