Difference Between Grep, Sed, and Awk - Baeldung On Linux
Difference Between Grep, Sed, and Awk - Baeldung On Linux
(/linux/) (https://www.baeldung.com/linux/)
x
1. Overview
In this article, we’ll go through the command-line tools grep
(https://man7.org/linux/man-pages/man1/grep.1.html), sed
(https://man7.org/linux/man-pages/man1/sed.1.html), and awk
(https://man7.org/linux/man-pages/man1/awk.1p.html). In particular, we’ll
study the di erences in functionality among them.
2. Background
https://www.baeldung.com/linux/grep-sed-awk-differences 1/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
When it comes to text processing in Linux, the three tools that come in pretty x
handy are grep, sed, and awk. Although being totally di erent tools, their
functionality seems to overlap in simple scenarios. For example, to nd a
pattern in a le and print the matches to the standard output, we’ll nd that all
of them can do that. x
However, if we stretch beyond this simple exercise, we’ll nd that grep
(/linux/common-text-search) is only good for simple text matching and
printing.
On the other hand, in addition to match and print text, sed (/linux/sed-editor)
o ers additional text transformation commands like substitution.
Finally, awk (/linux/awk-guide), being the most powerful of these tools, is a
scripting language that o ers a multitude of features that do not exists in
the former two.
Before we begin, it is important to know that the purpose of this article is to
make the distinction between these three tools clearer. Therefore, the
examples we are covering are just a small subset of what is possible with each
tool, especially in the case of sed and awk.
3. Text File
To facilitate our discussion, let’s de ne a text le log.txt:
4. grep
https://www.baeldung.com/linux/grep-sed-awk-differences 2/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
The grep command searches for lines matching a regex pattern and prints x
those matching lines to the standard output. It is useful when we need a
quick way to nd out whether a particular pattern exists or not in the given
input.
x
What happens here is that grep will scan through the lines in log.txt and print
those lines containing the word ERROR to the standard output.
When we execute the command above, grep will print every line in the
log.txt, except those lines matching the pattern INFO.
https://www.baeldung.com/linux/grep-sed-awk-differences 3/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
Sometimes, we may want to print the preceding or succeeding line around the
matchings. To print the ve lines after a match, we can use the ag -A:
x
grep -A 5 ERROR log.txt
On the other hand, to print the ve lines before a match, we can use the ag -
B:
Finally, the ag -C allows us to print both the ve lines before and the ve lines
after a match:
5. sed
The sed command is a stream editor that works on streams of characters. It’s a
more powerful tool than grep as it o ers more options for text processing
purposes, including the substitute command, which sed is most commonly
known for.
The OPTIONS are optional ags that can be applied on sed to modify its
behavior. Next, the SCRIPT argument is the sed script that will be executed on
every line for the les that are speci ed by the FILE argument.
[addr]X[options]
Where addr is the condition applied to the lines of the text le. It can be a xed x
number or a regex pattern that is tested against the content of a line before
processing it.
Next, the X character represents the sed command to execute. For example,
the substitute command, which is denoted with a single character.
Finally, additional options can be passed to the sed command to specify its
behavior.
By default, sed will print every line it is scanning to the standard output
stream. To disable this automatic printing, we can use the ag -n.
Next, it will run the script that comes after the ag -n and look for the regex
pattern ERROR on every line in log.txt. If there is a match, sed will print the line
to standard output because we’re using the p command in the script. Finally,
we pass log.txt as the name of the le we want sed to work on as the nal
argument.
's/pattern/replacement/'
When there is a match on a line for pattern, sed will substitute it with
replacement.
For example, if we want to substitute the word ERROR in our log.txt with the
word CRITICAL we can run:
https://www.baeldung.com/linux/grep-sed-awk-differences 5/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
If we want sed to persist the change on the le it is operating on, we can use
the ag -i along with a su x. Before making changes in place, sed will create a
backup of the le and append the su x to this backup lename. For instance,
when we run:
Here, sed will print the lines of log.txt starting from line number 3, and ending
when it nds the rst line that matches the pattern /ERROR/.
https://www.baeldung.com/linux/grep-sed-awk-differences 6/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
6. awk x
It will execute the script against every line in the le. Let’s now expand the
structure of the script:
'(pattern){action}'
The pattern is a regex pattern that will be tested against every input line. If a
line matches the pattern, awk will then execute the script de ned in action on
that line. If the pattern condition is absent, the action will be executed on
every line.
The code above will nd the regex pattern ERROR in the log.txt le and print
the matching line to the standard output.
The method gsub takes as arguments a regex pattern and the replacement
string. Then, awk print the line to the standard output.
https://www.baeldung.com/linux/grep-sed-awk-differences 8/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
In the script above, awk stores the counts of each distinct value Category
column in the variable count. Then the script prints the count value at the end.
https://www.baeldung.com/linux/grep-sed-awk-differences 9/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
From the output, we can see that the command only prints log lines that are
recorded later than the speci ed timestamp.
7. Conclusion
In this article, we started o with a basic introduction to grep, sed, and awk.
Then, we showed the usage of grep on simple text scanning and matching.
Next, we saw how sed is more useful than grep when we want to transform
our text.
Finally, we’ve demonstrated how awk is capable of replicating grep and sed
functionality while additionally providing more features for advanced text
processing.
{} [+]
https://www.baeldung.com/linux/grep-sed-awk-differences 10/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
x
1 COMMENT Oldest
CATEGORIES
ADMINISTRATION (/LINUX/CATEGORY/ADMINISTRATION)
FILES (/LINUX/CATEGORY/FILES)
FILESYSTEMS (/LINUX/CATEGORY/FILESYSTEMS)
INSTALLATION (/LINUX/CATEGORY/INSTALLATION)
NETWORKING (/LINUX/CATEGORY/NETWORKING)
PROCESSES (/LINUX/CATEGORY/PROCESSES)
SCRIPTING (/LINUX/CATEGORY/SCRIPTING)
SEARCH (/LINUX/CATEGORY/SEARCH)
SECURITY (/LINUX/CATEGORY/SECURITY)
WEB (/LINUX/CATEGORY/WEB)
SERIES
LINUX FILES (/LINUX/LINUX-FILES-SERIES)
LINUX SCRIPTING (/LINUX/LINUX-SCRIPTING-SERIES)
ABOUT
ABOUT BAELDUNG (/ABOUT)
THE FULL ARCHIVE (HTTPS://WWW.BAELDUNG.COM/LINUX/FULL_ARCHIVE)
EDITORS (HTTPS://WWW.BAELDUNG.COM/EDITORS)
https://www.baeldung.com/linux/grep-sed-awk-differences 11/12
1/25/2021 Difference Between grep, sed, and awk | Baeldung on Linux
https://www.baeldung.com/linux/grep-sed-awk-differences 12/12