Undoubtably grep (pronounced as /ɡɹɛp/) is the most powerful and highly useful command when it comes to working with terminal for a Unix/Unix-like Operating System's, be it the Apple's macOS or FreeBSD, Solaris, HP-UX or Linux. You got to understand this command right if you are a computer science student or working in the software industry. Most of the servers are deployed on Unix/Linux operating systems that can only be accessed through the command-line interface and if there are tons of files and you need to look for a specific one that you are looking at has a huge text in it and you want to search for specific text content? grep is the command that can save you in such a scenario.
Table of contents:
- What is a grep command?
- Some background of grep command.
- How to use grep?
- grep command examples
1. What is grep command?
If you open your terminal or bash shell and type "man grep" it will print out a manual of this command, let's see how the manual defines this term.
✏️ grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command.
Ok, so if you run this command in a particular directory it will the text-contents of the files for specific lines that follows a pattern using a specific word or words or something as complex as a Regular Expression provided. So grep is a command-line utility that will helps you search file contents!
The word grep is derived from ed's (command line editor for UNIX systems) command g/re/p - "globally search for a regular expression and print matching lines" so yeah! regExp is something you should be good with to make the best use of this command.
2. Some background of grep command.
grep was written by Ken Thompson, yes, the man who write the B Programming Language and implemented the original Unix operating system at the Bell Labs USA. He write it initially for his private use, but once when his manager Doug McIlroy came up with a requirement to build something very similar, he worked on this utility and fixed some bugs it had and presented it as grep the next day! :) so grep was build overnight! (Check out this youtube video at 35min to know more about "the grep story - of how it came out of his directory to bin directory" - https://www.youtube.com/watch?v=EY6q5dv_B-o)
As stated earlier grep has been ideated from ed text editor's g/re/p command, the ed editor as you must have guess was also written by Ken Thompson. Though ed had support for regular expressions it was not good enough to work with a large text fix, so Thompson selected that code into grep. He chose the name because in ed, the command g/re/p would print all lines matching a specified pattern.
3. How to use grep?
grep comes bundled with the Unix/Linux based operating system, you do not need to install it unless you are using a Windows computer. You may require tools like Bash for Windows or Git Bash to use this command.
Let's have a look at the syntax of the command first, we will try to again make use of the grep manual,
SYNOPSIS grep [OPTION...] PATTERNS [FILE...] grep [OPTION...] -e PATTERNS ... [FILE...] grep [OPTION...] -f PATTERN_FILE ... [FILE...]
Alright, so lets see the first one, grep followed by option in braces that means optional and file again in braces that's optional again. So leaving them aside I can simply try grep followed by a pattern: example: grep my-Word-to-search?
Ok, I have a dirctory call countries that has a file called counties_details.txt that contains list of all countries and major languages spoken there, let me try out grep Sweden followed by the path to the file.
$ grep 'Sweden' countries_details.txt Sweden Swedish, small Sami- and Finnish-speaking minorities
Cool, lets try some regular expressions, all the lines that starts with letter S: ^S.*
$ grep '^S.*' St. Kitts and Nevis English St. Lucia English (official), French patois St. Vincent and the Grenadines English, French patois San Marino Italian Saudi Arabia Arabic Senegal French (official); Wolof, Pulaar, Jola, Mandinka Serbia Serbian (official); Romanian, Hungarian, Slovak, and Croatian, Albanian Seychelles Seselwa Creole 92%, English 5%, French (all official) Singapore Mandarin 35%, English 23%, Malay 14.1%, Hokkien 11.4%, ..... Slovakia Slovak 84% (official), Hungarian 11%, Roma 2%, Ukrainian 1% (2001) Slovenia Slovenian 91%, Serbo-Croatian 5% (2002) South Africa IsiZulu 23.8%, IsiXhosa 17.6%, Afrikaans 13.3%, Sepedi 9.4%, . South Sudan English (official), Arabic (official), regional languages ...... Spain Castilian Spanish 74% (official); Catalan 17%, Galician 7%, Basque 2% Sri Lanka Sinhala 74% (official and national), Tamil 18% (national), other 8%; English by about 10% Suriname Dutch (official), Surinamese (lingua franca), English widely spoken, Hindustani, Javanese Swaziland English, siSwati (both official) Sweden Swedish, small Sami- and Finnish-speaking minorities Switzerland German 64%, French 20%, Italian 7% (all official); Romansch 0.5% (national) Syria Arabic (official); Kurdish, Armenian, Aramaic, Circassian widely understood; French, English
4. 10+ grep Command Examples:
I will try to put down as many grep examples that I can,
- Search for lines containing specific word: example: grep 'United' text-file-name
$ grep 'United' countries_details.txt United Arab Emirates Arabic (official), Persian, English, Hindi, Urdu United Kingdom English, Welsh, Scots Gaelic United States English 82%, Spanish 11%
- Search for lines containing specific word in all files in a directory: example: grep 'and' *
$ grep 'and' * Afghanistan Dari Persian, Pashtu, other Turkic and minor languages Angola Portuguese, Bantu and other African languages Antigua and Barbuda English, local dialects
- -i: Search with word insensitivity: example: grep -i 'denmark' text-file-name
$ grep -i 'denmark' countries_details.txt Denmark Danish, Faroese, Greenlandic, German, English
- -c: Get the count of the lines matched: example: grep -c 'United' text-file-name
$ grep -c 'United' countries_details.txt 3
- -o: Print only the matching part the line: example: grep -o 'United' text-file-name
$ grep -c 'United' countries_details.txt United United United
- -l: Print only filenames of the lines matched: example: grep -l 'United' *
$ grep -l 'United' * countries_details.txt database_dump.txt
- -n: Print matched text line with line numbers: example: grep -n 'United' *
$ grep -n 'United' * 186:United Arab Emirates Arabic (official), Persian, English, Hindi, Urdu 187:United Kingdom English, Welsh, Scots Gaelic 188:United States English 82%, Spanish 11%
- -v: Print lines that did not match the patterns: example: grep -v 'and' *
$ grep -v 'and' * Samoa Samoan, English San Marino Italian Saudi Arabia Arabic
- -e: Using multiple (regex) Expressions at once with grep: example: grep -e 'United' -e 'Sweden' -e 'Australia'
$ grep -e 'United' -e 'Sweden' -e 'Australia' countries_details.txt Australia English 79%, native and other languages Sweden Swedish, small Sami- and Finnish-speaking minorities United Arab Emirates Arabic , Persian, English, Hindi, Urdu United Kingdom English, Welsh, Scots Gaelic United States English 82%, Spanish 11%
- -w: Whole word match: example: grep -w 'English,' file-name
$ grep -w 'English,'countries_details.txt Argentina Spanish (official), English, Italian, German, French Brazil Portuguese (official), Spanish, English, French Greece Greek 99% (official), English, French Iceland Icelandic, English, Nordic languages, German widely spoken India Hindi 30%, English, Bengali, Gujarati, Malayalam, Marathi, Oriya, Punjabi, Tamil, Telugu, Urdu, Kannada, Sanskrit Indonesia Bahasa Indonesia (official), English, Dutch, Javanese, and more than 580 other languages and dialects Jamaica English, Jamaican Creole Lebanon Arabic (official), French, English, Armenian Lesotho English, Sesotho (both official); Zulu, Xhosa New Zealand English, Maori (both official) United Kingdom English, Welsh, Scots Gaelic
- Background task activation is spurious error - Windows 10, Office 2016, Office 365
- How to Generate Self-Signed OpenSSL certificate in three easy steps
- How to Kill service running on port using terminal command
- How to press shortcut CTRL + ALT + DEL on Windows Remote Desktop
- Top 10 emerging breakthrough trending technologies
- Free Unlimited Calls from MTNL & BSNL Landlines from 1st May 2015
- Setting up RSS feeds notifications within Outlook
- Static IP MTNL available at Rs. 200 per month
- CentOS Cannot find a valid baseurl for repo base7x86_64 yum
- How to write hello world different languages syntax
- BSNL Broadband upgrades speed to minimum 2MBps for all users 512Kbps 1Mbps
- [IRCTC] Indian railways official eRail API 1.1 for developers to get train info
- How to submit website to dmoz directory
- Gmail Unable to upload because it is a folder or a package (like an application bundle or RTFD document)
- How to use HashTags # correctly on Social Media Facebook Twitter
- Copy all .vcf Mobile Contacts files into one .vcf
- How to Change or Set a new Password for your Wi-Fi Router
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK - Android
- Building library Gradle Project Info: Downloading services.gradle.org - Android-Studio
- Turn Off Google Analytics intelligence Alert Emails - Google
- MySQL Error :1006 SQLSTATE: HY000 (ER_CANT_CREATE_DB) Message: Can't create database 'mydb' (errno: 28) - MySQL
- Replace tabs by spaces or comma Notepad++ - NotepadPlusPlus
- PowerShell iterate and get all SharePoint sites and sub sites - SharePoint
- Turn on off volume change button sounds Mac OS X - Mac-OS-X
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian) - Android
- How to Detect Phone Shakes Android Programming - Android
- MySQL : Error :1000 SQLSTATE: HY000 (ER_HASHCHK) Message: hashchk Server Error - MySQL
- How to add border to Android Button - Android
- How to Center Align Image in Bootstrap - CSS
- Recover SharePoint if the SQL Server IP changes - SharePoint
- Google Chrome Update failed (error: 12) - Chrome
- Send Email using SharePoint PowerShell command - SharePoint