Grep command is one very useful command in Linux. It allows you to search for a specific pattern trough dozens of files or directories. Alongside with pipe (|), it can become very handy when it comes for specific task or need to display searched terms and etc. In this article, check out this several grep command samples.
First of all what is grep?
Grep command is a command that searches for pattern in each line and print's it on standard output. It stands for global regular expression print and as noted in short intro, it can become very useful when in need. Here are several samples.
1. Search for the given string in a single file
In this sample, grep command shall display string "this" out of grepfile.txt. Heres the command.
# grep "this" grepfile.txt
As you can see above, it has displayed all the lines which contain string "this".
2. Check for the given string in multiple files
In this example, grep command is searching for the string "this" in multiple files. Here's the command.
# grep "this" grepfile.txt *
Note that in above figure, grep command has searched for string "this" inside a grepfile.txt and grepfile1.txt and displayed search results on to standard output.
3. Case sensitive search using grep command
In this example, each line containing the "this", "This", or "THIS" string shall be displayed in standard output. Here's the command.
# grep -i "the" grepfile.txt
4. Match regular expression in files
In this example, grep will search for all the lines that begin with "lines" and end on "empty" in a file grepfile.txt. Here's the command.
# grep "lines.*empty" grepfile.txt
5. Checking for full word using grep command
The following command will display all the lines that contains the word 'Ahhiyawa' in a demofile1. Here's the command.
# grep -iw "Ahhiyawa" demofile1
6. Search for word recursively
In this example, grep is going to search for string 'amar' inside a /etc/passwd file. Here's the command.
# grep -r "amar" /etc/passwd
7. Search for two different words
In this example, egrep command is being used to display two words inside a file stream.txt. Here's the command.
# egrep -w "Zorin|Pop" stream.txt
8. Display cpu model out of /proc file
In this example, grep command will display cpu model out of /proc file. Here's the command.
# cat /proc/cpuinfo | grep -i 'Model'
9. List all the files containing the searching string
In this example, grep command shall display the file containing the "letter" string. Here's the command.
# grep "letter" /home/amar *
10. Show lines number when displaying output
In this example, grep command will display the line number alongside the output file. Here's the command.
# grep -n "amar" /etc/passwd
There you go. These are several useful grep command examples in Linux. If you like this article, please share it and leave the comments down bellow. If you wanna know more, than visit the following links.
[1] 12 practical examples of grep command in Linux
https://www.tecmint.com/12-practical-examples-of-linux-grep-command/
[2] How to search a file for text string in Linux?
https://www.cyberciti.biz/faq/howto-search-find-file-for-text-string/
Nema komentara:
Objavi komentar