How-to: Use Grep on Unix Systems to Search Like a Pro
2024-04-15 | By Maker.io Staff
The Unix grep command is a powerful tool for working with textual data, and the built-in command-line program has more to offer than one might initially imagine. This article covers everything newcomers to the command line need to know to get up and running using grep.
Finding Files that Contain a Specific String Using Grep
The tool’s most basic functions include searching strings in files, which can be helpful for finding all files that contain the given keyword, such as all recipes in a folder that require a specific ingredient:
Copy Code
grep “ingredient_name” files_to_search
Suppose you wanted to find all of your recipes that require butter. You could use grep to search for occurrences of the ingredient in all files within the current working directory. Note that the search is case-sensitive by default, meaning that grep only considers results that exactly match the search term character by character:
This image demonstrates how to find all files that contain the word “Butter.”
In the example above, the search returns the four results consisting of three distinct files containing the search term “Butter” in one form or another. Note that the same search with a lowercase b does not return any results. However, you can add the -i option to grep to make the program ignore the case when searching:
Adding the -i flag instructs grep to ignore the case when searching for a keyword.
The results will still include ingredients that contain the term “butter” but are not exact matches. You can make grep only consider exact matches by enforcing that the program only considers whole words using the -w option, as illustrated by the following example:
The additional -w option makes grep only consider matches that contain the entire word in isolation.
Finally, as the search term is known, you may only be interested in the file name itself. By default, grep also prints the expression that made it include a file in the result list. You can turn off this behavior by adding the -l option (lowercase L). In addition, displaying the line number (-n) can be helpful when the word occurs in a long document. Finally, you can extend the search to all sub-folders in a directory by adding the -r flag to the grep call.
Using the Unix Pipe Operation to Combine Grep with Other Commands
While finding text files that contain a term is a convenient feature, grep truly shines when combined with other Linux programs. As discussed in another article, pipes allow chaining multiple commands, including grep. Therefore, grep can filter the output of one program before passing it to another command as input. Alternatively, grep can also serve as an output filter for removing unwanted details from the final output before storing it in a file or presenting it to users.
Suppose you wanted to know whether python3.11 is installed on your system. You can use a package manager, such as apt, to list all installed packages and then pass the result into grep to remove all other packages:
Grep is a valuable tool for filtering program output and making the output more concise and manageable.
Note that the grep call now does not contain any file names. Calling grep without a file name leads to the program reading the contents on the system’s standard input channel, which comes from another program in this example. Either way, the grep call additionally contains the -E option and the search term looks more exotic than before, as it now represents a regular expression with so-called wildcards.
Understanding Wildcards and Placeholders in Grep
In simple terms, a regular expression (often abbreviated to regex) is a sequence of characters that represents a pattern to apply in searches. The pattern can define that parts of the search term are irrelevant, that a term should begin or end a certain way, or that specific parts in the term should repeat multiple times, to name a few options. Without going into too much detail, some essential patterns include:
Note that the table only lists a few examples of regular expressions, and you can combine the patterns to build complex search terms to find precisely what you’re looking for. For example:
Copy Code
grep --help | grep -E ".*NUM.*context$"
This command prints the grep help that explains all possible options and then filters the output to include only the ones that contain ‘NUM’ and end with the word ‘context’:
More complex regular expressions can help users find precisely what they’re looking for.
Summary
Grep is a powerful tool for working with textual data. The program allows searching texts and regular expression patterns in files and output generated by other Linux commands. Therefore, the program can aid you in finding files that contain the specified keywords and filtering program output to remove irrelevant information.
Aside from keyword searches, grep also supports regular expressions and wildcards that define how the search term has to begin or end for the program to report a match. The article discussed only a few common patterns for you to try when defining search strings.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.