What is a Unix grep Command and how does it relate to the Mac Terminal and Windows PowerShell?

Unix is an operating system. It supports multitasking and multi-user functionality. Unix is most widely used in all forms of computing systems such as desktop, laptop, and servers. On Unix, there is a Graphical user interface similar to windows that support easy navigation and support environment.

The top reason for the popularity of Unix Shell Scripting is its robust scope and command line automation. It’s a powerful programming method that can help you learn the command-line better, and makes it easy to understand basic Mac Terminal and Windows PowerShell commands. PowerShell brings the functionality of grep equivalent with the Select-String cmdlet.

grep is a Unix command-line utility for searching plain-text data sets for lines that match a regular expression. Think of it a “search” or “find” command in Unix. Most basic Unix command forms like grep relate to the present Mac Terminal and Windows PowerShell commands. PowerShell is superficially similar to Unix shells. PowerShell has aliases for many of the commands you are used to in Unix, like ls, rm, cp, mv, etc.

The grep command is a filter that is used to search for lines matching a specified pattern and print the matching lines to standard output.

The syntax is:

grep ‘<text-to-be-searched>’ <file/files>

Note that single or double quotes are required around the text if it is more than one word. You can also use the wildcard (*) to select all files in a directory.

If its a one word search, example below (serach for the name “financial” in a file called “advisor”):

grep financial advisor

Grep will display every line where there is a match for the word financial. When executing this command, you do not get exact matches. Instead, the terminal prints the lines with words containing the string of characters you entered.

Say you want to search all files in a Directory. How do you do that?

To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command.

Example, we use financial as a search criterion:

grep financial *

A good UNIX For Dummies Cheat Sheet is here. Unix operates and command line syntx carries over into other OS like MacOS and Windows. Its a good to review how Unix basic commands work when trying them out on your Mac and Windows equivalent automation manager.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s