Verified and Tested 08/31/15
Few things are more frustrating than wanting to do something and not knowing how to do it. In this How-to, we will be going over the GREP commands so we could make our lives easier and work more effective in our sessions.
In This Article
- GREP in a Nutshell
- Searching a Single File
- Searching Multiple Files
- Searching and Ignoring Files
- Counting Words in a Specified File
- Searching Before and After
- Searching Patterns
- Searching Case Sensitive Words
- Searching Patterns in gzip Files
- Searching for Whole Words
- Showing Specified File Names
- Showing Line Numbers
- Recursive Search
- To View a File
You need a Linux server that is configured with a static IP address. If you do not have a server already, you can visit VPS hosting page and spin a new server up in under 30 seconds.
GREP in a Nutshell
To understand how to work in GREP, we need to know what GREP is and what it does. An acronym for “Global Regular Expression Print”, GREP is a command that allows you to manipulate the way requested information is printed/viewed.
Searching a Single File
With the following grep command you can search for a single file from a specified location. It is simply read as getting MyText from MyFile, and the command reads as follows.
grep "MyText" MyFile
Searching Multiple Files
With the following grep command you can search for multiple files from a specified location.Its is simply read as get MyText from MyFile in any format(tis could be TXT, JPEG, PHP, etc.)
grep "MyText" MyFile_txt
Searching and Ignoring Files
With the following command, you could search for specific files at the same time ignore irrelevant ones. MyFile being the file that you want and the second grep text after the pipe is the one that is irrelevant, and you want to be ignored.
grep MyFile | grep -v IrrelevantFile
Counting Words in a Specified File
With the -c variable, you can count how many of the same word or phrase in a specific file. Want to know how many times MyWord appears in myfile.txt?
grep -c "MyWord" myfile.txt
Searching Before and After
With the –context= and -C command we can search words before and after a specified words or phrases in specific locations. Want to know how many lines are before and after MyWord?
grep --context=3 MyWord MyFile.txt grep -C 3 'MyWord' MyFile.txt
With the egrep command, we can do an extended search using | (pipe) to search for wanted and unwanted words. Want to know where is a line that specifies multilple words from MyFile.txt?
egrep 'UnwatedWord|WantedWord' MyFile.txt
Searching Case Sensitive Words
With the -i command we can find a specified word no matter if it’s in upper case or lower case letters. Want to know where is MyWord no matter how its written?
grep -i MyWord MyFile
Searching Patterns in gzip Files
With the zgrep command, we can find a specified word no matter if it’s in upper case or lower case letters in the any .gz file. Want to know where is MyWord no matter how its written in all of my .gz files?
zgrep -i MyWord *.gz
Searching for Whole Words
With the -w command, we can find whole specified words displaying their whole line. Want to know the lines that contain is MyWord withing MyFile?
grep -w MyWord MyFile.txt
To search a word ending with MyWord, anywhere, Run the following command:
grep 'MyWord>' *
Showing Specified File Names
With this – l command we can see all files that end without specified name. In this case .myfile. You could accomplish this with the following command:
grep -l 'main' *.myfile
Showing Line Numbers
With the -n command, we can view all of the numbers in lines of the specified words that the error appeared.
grep -n MyWord lg Myfile
With the -R command, you will be able to see all files in any directories and subdirectories.
grep -R store*
To View a File
With the –color command, you could also search for a specific word and display it in color for easy reading.
grep --color MyWord MyFile.txt
Congratulations! This completes this tutorial on GREP Commands. We hope that you found this information useful just like it was to me. Thank you for following along! Check back with us for further updates and try any of our top VPS hosting solutions.