Grep is a unix search utility used to return lines from a file(s) that match a particular search term. Sed is also a unix terminal utility, but is used for string replacement. Power users will combine the search functionalities of Grep, with the replacement abilities of Sed for efficient text manipulation orders of magnitude faster than say, a Python script, in some cases.

Table of contents

Grep command syntax

grep -i error logfile.txt

This command performs a case-insensitive search (-i) for the term “error” in the file “logfile.txt”. The output will include any line where the search term is found.

Output example:

Error 500, Internal server problem
Error 404, Page could not be found
Error 404, Page could not be found

Sed command syntax

sed s/java/ruby/g myfile.txt
Sed is commonly used without any command-line options, thus none are presented in this graphic for clarity

If the contents of myfile.txt were as follows:

Java is the best programming language,
thousands of programmers use Java everyday.

Running the above Sed command would produce the following output:

Ruby is the best programming language,
thousands of programmers use Ruby everyday.

How to combine Grep and Sed

The real power comes when you use Grep and Sed together. If no input file is provided to Sed, it with operate on the contents of standard input. This is the basis for how Grep and Sed are combined: the pipe character, |, turns the output of Grep into the standard input for Sed.

Grep & Sed syntax

grep -i error log.txt | sed s/Error/Status/g

If the output of just the Grep command (grep -i "error" log.txt) above produced the following:

Error 500, Internal server problem
Error 404, Page could not be found
Error 404, Page could not be found

Running the entire combined Grep and Sed statement would result in the following being written to standard output:

Status 500, Internal server problem
Status 404, Page could not be found
Status 404, Page could not be found

Write the output to a file

The basic example above just prints to standard output. To write the output of the grep and sed command to a file instead, use >:

grep -i "error" log.txt | sed "s/Error/Status/g" > error_logs.txt

Multiple files

A more realistic version of this entire command would be funneling error lines from all files in a directory (in this example “logs” is a directory) into a single file, plus transforming the resulting lines from comma-separated to semi-colon separated.

grep -ri "error" logs | sed "s/,/;/g" > error_logs.txt

More Grep & Sed usecases

These basic examples are a bit contrived for the sake of clarity. When Grep and Sed are utilized together in practice, higher volumes of data are typically involved, along with more advanced text manipulation than simply find/replace. After all, the benefit of utilizing Grep & Sed instead of the text search/manipulation built into your IDE is processing speed when large amounts of data, or manipulation, are involved.

The scope of this article is purposfully focused on how Grep and Sed can be linked together; however, both utilities can do much more than this article’s trivial examples. By making use of command-line options and RegExs (for example), the power of both Grep & Sed individually and combined is enormous.

I highly recommend the following resources to start learning more: